ipath_eeprom.c

来自「linux 内核源代码」· C语言 代码 · 共 889 行 · 第 1/2 页

C
889
字号
/* * Copyright (c) 2006, 2007 QLogic Corporation. All rights reserved. * Copyright (c) 2003, 2004, 2005, 2006 PathScale, Inc. All rights reserved. * * This software is available to you under a choice of one of two * licenses.  You may choose to be licensed under the terms of the GNU * General Public License (GPL) Version 2, available from the file * COPYING in the main directory of this source tree, or the * OpenIB.org BSD license below: * *     Redistribution and use in source and binary forms, with or *     without modification, are permitted provided that the following *     conditions are met: * *      - Redistributions of source code must retain the above *        copyright notice, this list of conditions and the following *        disclaimer. * *      - Redistributions in binary form must reproduce the above *        copyright notice, this list of conditions and the following *        disclaimer in the documentation and/or other materials *        provided with the distribution. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */#include <linux/delay.h>#include <linux/pci.h>#include <linux/vmalloc.h>#include "ipath_kernel.h"/* * InfiniPath I2C driver for a serial eeprom.  This is not a generic * I2C interface.  For a start, the device we're using (Atmel AT24C11) * doesn't work like a regular I2C device.  It looks like one * electrically, but not logically.  Normal I2C devices have a single * 7-bit or 10-bit I2C address that they respond to.  Valid 7-bit * addresses range from 0x03 to 0x77.  Addresses 0x00 to 0x02 and 0x78 * to 0x7F are special reserved addresses (e.g. 0x00 is the "general * call" address.)  The Atmel device, on the other hand, responds to ALL * 7-bit addresses.  It's designed to be the only device on a given I2C * bus.  A 7-bit address corresponds to the memory address within the * Atmel device itself. * * Also, the timing requirements mean more than simple software * bitbanging, with readbacks from chip to ensure timing (simple udelay * is not enough). * * This all means that accessing the device is specialized enough * that using the standard kernel I2C bitbanging interface would be * impossible.  For example, the core I2C eeprom driver expects to find * a device at one or more of a limited set of addresses only.  It doesn't * allow writing to an eeprom.  It also doesn't provide any means of * accessing eeprom contents from within the kernel, only via sysfs. */enum i2c_type {	i2c_line_scl = 0,	i2c_line_sda};enum i2c_state {	i2c_line_low = 0,	i2c_line_high};#define READ_CMD 1#define WRITE_CMD 0static int eeprom_init;/* * The gpioval manipulation really should be protected by spinlocks * or be converted to use atomic operations. *//** * i2c_gpio_set - set a GPIO line * @dd: the infinipath device * @line: the line to set * @new_line_state: the state to set * * Returns 0 if the line was set to the new state successfully, non-zero * on error. */static int i2c_gpio_set(struct ipath_devdata *dd,			enum i2c_type line,			enum i2c_state new_line_state){	u64 out_mask, dir_mask, *gpioval;	unsigned long flags = 0;	gpioval = &dd->ipath_gpio_out;	if (line == i2c_line_scl) {		dir_mask = dd->ipath_gpio_scl;		out_mask = (1UL << dd->ipath_gpio_scl_num);	} else {		dir_mask = dd->ipath_gpio_sda;		out_mask = (1UL << dd->ipath_gpio_sda_num);	}	spin_lock_irqsave(&dd->ipath_gpio_lock, flags);	if (new_line_state == i2c_line_high) {		/* tri-state the output rather than force high */		dd->ipath_extctrl &= ~dir_mask;	} else {		/* config line to be an output */		dd->ipath_extctrl |= dir_mask;	}	ipath_write_kreg(dd, dd->ipath_kregs->kr_extctrl, dd->ipath_extctrl);	/* set output as well (no real verify) */	if (new_line_state == i2c_line_high)		*gpioval |= out_mask;	else		*gpioval &= ~out_mask;	ipath_write_kreg(dd, dd->ipath_kregs->kr_gpio_out, *gpioval);	spin_unlock_irqrestore(&dd->ipath_gpio_lock, flags);	return 0;}/** * i2c_gpio_get - get a GPIO line state * @dd: the infinipath device * @line: the line to get * @curr_statep: where to put the line state * * Returns 0 if the line was set to the new state successfully, non-zero * on error.  curr_state is not set on error. */static int i2c_gpio_get(struct ipath_devdata *dd,			enum i2c_type line,			enum i2c_state *curr_statep){	u64 read_val, mask;	int ret;	unsigned long flags = 0;	/* check args */	if (curr_statep == NULL) {		ret = 1;		goto bail;	}	/* config line to be an input */	if (line == i2c_line_scl)		mask = dd->ipath_gpio_scl;	else		mask = dd->ipath_gpio_sda;	spin_lock_irqsave(&dd->ipath_gpio_lock, flags);	dd->ipath_extctrl &= ~mask;	ipath_write_kreg(dd, dd->ipath_kregs->kr_extctrl, dd->ipath_extctrl);	/*	 * Below is very unlikely to reflect true input state if Output	 * Enable actually changed.	 */	read_val = ipath_read_kreg64(dd, dd->ipath_kregs->kr_extstatus);	spin_unlock_irqrestore(&dd->ipath_gpio_lock, flags);	if (read_val & mask)		*curr_statep = i2c_line_high;	else		*curr_statep = i2c_line_low;	ret = 0;bail:	return ret;}/** * i2c_wait_for_writes - wait for a write * @dd: the infinipath device * * We use this instead of udelay directly, so we can make sure * that previous register writes have been flushed all the way * to the chip.  Since we are delaying anyway, the cost doesn't * hurt, and makes the bit twiddling more regular */static void i2c_wait_for_writes(struct ipath_devdata *dd){	(void)ipath_read_kreg32(dd, dd->ipath_kregs->kr_scratch);	rmb();}static void scl_out(struct ipath_devdata *dd, u8 bit){	udelay(1);	i2c_gpio_set(dd, i2c_line_scl, bit ? i2c_line_high : i2c_line_low);	i2c_wait_for_writes(dd);}static void sda_out(struct ipath_devdata *dd, u8 bit){	i2c_gpio_set(dd, i2c_line_sda, bit ? i2c_line_high : i2c_line_low);	i2c_wait_for_writes(dd);}static u8 sda_in(struct ipath_devdata *dd, int wait){	enum i2c_state bit;	if (i2c_gpio_get(dd, i2c_line_sda, &bit))		ipath_dbg("get bit failed!\n");	if (wait)		i2c_wait_for_writes(dd);	return bit == i2c_line_high ? 1U : 0;}/** * i2c_ackrcv - see if ack following write is true * @dd: the infinipath device */static int i2c_ackrcv(struct ipath_devdata *dd){	u8 ack_received;	/* AT ENTRY SCL = LOW */	/* change direction, ignore data */	ack_received = sda_in(dd, 1);	scl_out(dd, i2c_line_high);	ack_received = sda_in(dd, 1) == 0;	scl_out(dd, i2c_line_low);	return ack_received;}/** * wr_byte - write a byte, one bit at a time * @dd: the infinipath device * @data: the byte to write * * Returns 0 if we got the following ack, otherwise 1 */static int wr_byte(struct ipath_devdata *dd, u8 data){	int bit_cntr;	u8 bit;	for (bit_cntr = 7; bit_cntr >= 0; bit_cntr--) {		bit = (data >> bit_cntr) & 1;		sda_out(dd, bit);		scl_out(dd, i2c_line_high);		scl_out(dd, i2c_line_low);	}	return (!i2c_ackrcv(dd)) ? 1 : 0;}static void send_ack(struct ipath_devdata *dd){	sda_out(dd, i2c_line_low);	scl_out(dd, i2c_line_high);	scl_out(dd, i2c_line_low);	sda_out(dd, i2c_line_high);}/** * i2c_startcmd - transmit the start condition, followed by address/cmd * @dd: the infinipath device * @offset_dir: direction byte * *      (both clock/data high, clock high, data low while clock is high) */static int i2c_startcmd(struct ipath_devdata *dd, u8 offset_dir){	int res;	/* issue start sequence */	sda_out(dd, i2c_line_high);	scl_out(dd, i2c_line_high);	sda_out(dd, i2c_line_low);	scl_out(dd, i2c_line_low);	/* issue length and direction byte */	res = wr_byte(dd, offset_dir);	if (res)		ipath_cdbg(VERBOSE, "No ack to complete start\n");	return res;}/** * stop_cmd - transmit the stop condition * @dd: the infinipath device * * (both clock/data low, clock high, data high while clock is high) */static void stop_cmd(struct ipath_devdata *dd){	scl_out(dd, i2c_line_low);	sda_out(dd, i2c_line_low);	scl_out(dd, i2c_line_high);	sda_out(dd, i2c_line_high);	udelay(2);}/** * eeprom_reset - reset I2C communication * @dd: the infinipath device */static int eeprom_reset(struct ipath_devdata *dd){	int clock_cycles_left = 9;	u64 *gpioval = &dd->ipath_gpio_out;	int ret;	unsigned long flags;	spin_lock_irqsave(&dd->ipath_gpio_lock, flags);	/* Make sure shadows are consistent */	dd->ipath_extctrl = ipath_read_kreg64(dd, dd->ipath_kregs->kr_extctrl);	*gpioval = ipath_read_kreg64(dd, dd->ipath_kregs->kr_gpio_out);	spin_unlock_irqrestore(&dd->ipath_gpio_lock, flags);	ipath_cdbg(VERBOSE, "Resetting i2c eeprom; initial gpioout reg "		   "is %llx\n", (unsigned long long) *gpioval);	eeprom_init = 1;	/*	 * This is to get the i2c into a known state, by first going low,	 * then tristate sda (and then tristate scl as first thing	 * in loop)	 */	scl_out(dd, i2c_line_low);	sda_out(dd, i2c_line_high);	while (clock_cycles_left--) {		scl_out(dd, i2c_line_high);		if (sda_in(dd, 0)) {			sda_out(dd, i2c_line_low);			scl_out(dd, i2c_line_low);			ret = 0;			goto bail;		}		scl_out(dd, i2c_line_low);	}	ret = 1;bail:	return ret;}/** * ipath_eeprom_read - receives bytes from the eeprom via I2C * @dd: the infinipath device * @eeprom_offset: address to read from * @buffer: where to store result * @len: number of bytes to receive */static int ipath_eeprom_internal_read(struct ipath_devdata *dd,					u8 eeprom_offset, void *buffer, int len){	/* compiler complains unless initialized */	u8 single_byte = 0;	int bit_cntr;	int ret;	if (!eeprom_init)		eeprom_reset(dd);	eeprom_offset = (eeprom_offset << 1) | READ_CMD;	if (i2c_startcmd(dd, eeprom_offset)) {		ipath_dbg("Failed startcmd\n");		stop_cmd(dd);		ret = 1;		goto bail;	}	/*	 * eeprom keeps clocking data out as long as we ack, automatically	 * incrementing the address.	 */	while (len-- > 0) {		/* get data */		single_byte = 0;		for (bit_cntr = 8; bit_cntr; bit_cntr--) {			u8 bit;			scl_out(dd, i2c_line_high);			bit = sda_in(dd, 0);			single_byte |= bit << (bit_cntr - 1);			scl_out(dd, i2c_line_low);		}		/* send ack if not the last byte */		if (len)			send_ack(dd);		*((u8 *) buffer) = single_byte;		buffer++;	}	stop_cmd(dd);	ret = 0;bail:	return ret;}/** * ipath_eeprom_write - writes data to the eeprom via I2C * @dd: the infinipath device * @eeprom_offset: where to place data * @buffer: data to write * @len: number of bytes to write */static int ipath_eeprom_internal_write(struct ipath_devdata *dd, u8 eeprom_offset,				       const void *buffer, int len){	u8 single_byte;	int sub_len;	const u8 *bp = buffer;	int max_wait_time, i;	int ret;	if (!eeprom_init)		eeprom_reset(dd);	while (len > 0) {		if (i2c_startcmd(dd, (eeprom_offset << 1) | WRITE_CMD)) {			ipath_dbg("Failed to start cmd offset %u\n",				  eeprom_offset);			goto failed_write;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?