USB: add Cypress c67x00 low level interface code
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / usb / c67x00 / c67x00-ll-hpi.c
blobe921991bc36a138bcfc15be00d019e9fc06360f7
1 /*
2 * c67x00-ll-hpi.c: Cypress C67X00 USB Low level interface using HPI
4 * Copyright (C) 2006-2008 Barco N.V.
5 * Derived from the Cypress cy7c67200/300 ezusb linux driver and
6 * based on multiple host controller drivers inside the linux kernel.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
21 * MA 02110-1301 USA.
24 #include <asm/byteorder.h>
25 #include <linux/io.h>
26 #include <linux/usb/c67x00.h>
27 #include "c67x00.h"
29 #define COMM_REGS 14
31 struct c67x00_lcp_int_data {
32 u16 regs[COMM_REGS];
35 /* -------------------------------------------------------------------------- */
36 /* Interface definitions */
38 #define COMM_ACK 0x0FED
39 #define COMM_NAK 0xDEAD
41 #define COMM_RESET 0xFA50
42 #define COMM_EXEC_INT 0xCE01
43 #define COMM_INT_NUM 0x01C2
45 /* Registers 0 to COMM_REGS-1 */
46 #define COMM_R(x) (0x01C4 + 2 * (x))
48 #define HUSB_SIE_pCurrentTDPtr(x) ((x) ? 0x01B2 : 0x01B0)
49 #define HUSB_SIE_pTDListDone_Sem(x) ((x) ? 0x01B8 : 0x01B6)
50 #define HUSB_pEOT 0x01B4
52 /* Software interrupts */
53 /* 114, 115: */
54 #define HUSB_SIE_INIT_INT(x) ((x) ? 0x0073 : 0x0072)
55 #define HUSB_RESET_INT 0x0074
57 #define SUSB_INIT_INT 0x0071
58 #define SUSB_INIT_INT_LOC (SUSB_INIT_INT * 2)
60 /* -----------------------------------------------------------------------
61 * HPI implementation
63 * The c67x00 chip also support control via SPI or HSS serial
64 * interfaces. However, this driver assumes that register access can
65 * be performed from IRQ context. While this is a safe assuption with
66 * the HPI interface, it is not true for the serial interfaces.
69 /* HPI registers */
70 #define HPI_DATA 0
71 #define HPI_MAILBOX 1
72 #define HPI_ADDR 2
73 #define HPI_STATUS 3
75 static inline u16 hpi_read_reg(struct c67x00_device *dev, int reg)
77 return __raw_readw(dev->hpi.base + reg * dev->hpi.regstep);
80 static inline void hpi_write_reg(struct c67x00_device *dev, int reg, u16 value)
82 __raw_writew(value, dev->hpi.base + reg * dev->hpi.regstep);
85 static inline u16 hpi_read_word_nolock(struct c67x00_device *dev, u16 reg)
87 hpi_write_reg(dev, HPI_ADDR, reg);
88 return hpi_read_reg(dev, HPI_DATA);
91 static u16 hpi_read_word(struct c67x00_device *dev, u16 reg)
93 u16 value;
94 unsigned long flags;
96 spin_lock_irqsave(&dev->hpi.lock, flags);
97 value = hpi_read_word_nolock(dev, reg);
98 spin_unlock_irqrestore(&dev->hpi.lock, flags);
100 return value;
103 static void hpi_write_word_nolock(struct c67x00_device *dev, u16 reg, u16 value)
105 hpi_write_reg(dev, HPI_ADDR, reg);
106 hpi_write_reg(dev, HPI_DATA, value);
109 static void hpi_write_word(struct c67x00_device *dev, u16 reg, u16 value)
111 unsigned long flags;
113 spin_lock_irqsave(&dev->hpi.lock, flags);
114 hpi_write_word_nolock(dev, reg, value);
115 spin_unlock_irqrestore(&dev->hpi.lock, flags);
119 * Only data is little endian, addr has cpu endianess
121 static void hpi_write_words_le16(struct c67x00_device *dev, u16 addr,
122 u16 *data, u16 count)
124 unsigned long flags;
125 int i;
127 spin_lock_irqsave(&dev->hpi.lock, flags);
129 hpi_write_reg(dev, HPI_ADDR, addr);
130 for (i = 0; i < count; i++)
131 hpi_write_reg(dev, HPI_DATA, cpu_to_le16(*data++));
133 spin_unlock_irqrestore(&dev->hpi.lock, flags);
137 * Only data is little endian, addr has cpu endianess
139 static void hpi_read_words_le16(struct c67x00_device *dev, u16 addr,
140 u16 *data, u16 count)
142 unsigned long flags;
143 int i;
145 spin_lock_irqsave(&dev->hpi.lock, flags);
146 hpi_write_reg(dev, HPI_ADDR, addr);
147 for (i = 0; i < count; i++)
148 *data++ = le16_to_cpu(hpi_read_reg(dev, HPI_DATA));
150 spin_unlock_irqrestore(&dev->hpi.lock, flags);
153 static void hpi_set_bits(struct c67x00_device *dev, u16 reg, u16 mask)
155 u16 value;
156 unsigned long flags;
158 spin_lock_irqsave(&dev->hpi.lock, flags);
159 value = hpi_read_word_nolock(dev, reg);
160 hpi_write_word_nolock(dev, reg, value | mask);
161 spin_unlock_irqrestore(&dev->hpi.lock, flags);
164 static void hpi_clear_bits(struct c67x00_device *dev, u16 reg, u16 mask)
166 u16 value;
167 unsigned long flags;
169 spin_lock_irqsave(&dev->hpi.lock, flags);
170 value = hpi_read_word_nolock(dev, reg);
171 hpi_write_word_nolock(dev, reg, value & ~mask);
172 spin_unlock_irqrestore(&dev->hpi.lock, flags);
175 static u16 hpi_recv_mbox(struct c67x00_device *dev)
177 u16 value;
178 unsigned long flags;
180 spin_lock_irqsave(&dev->hpi.lock, flags);
181 value = hpi_read_reg(dev, HPI_MAILBOX);
182 spin_unlock_irqrestore(&dev->hpi.lock, flags);
184 return value;
187 static u16 hpi_send_mbox(struct c67x00_device *dev, u16 value)
189 unsigned long flags;
191 spin_lock_irqsave(&dev->hpi.lock, flags);
192 hpi_write_reg(dev, HPI_MAILBOX, value);
193 spin_unlock_irqrestore(&dev->hpi.lock, flags);
195 return value;
198 u16 c67x00_ll_hpi_status(struct c67x00_device *dev)
200 u16 value;
201 unsigned long flags;
203 spin_lock_irqsave(&dev->hpi.lock, flags);
204 value = hpi_read_reg(dev, HPI_STATUS);
205 spin_unlock_irqrestore(&dev->hpi.lock, flags);
207 return value;
210 void c67x00_ll_hpi_reg_init(struct c67x00_device *dev)
212 int i;
214 hpi_recv_mbox(dev);
215 c67x00_ll_hpi_status(dev);
216 hpi_write_word(dev, HPI_IRQ_ROUTING_REG, 0);
218 for (i = 0; i < C67X00_SIES; i++) {
219 hpi_write_word(dev, SIEMSG_REG(i), 0);
220 hpi_read_word(dev, SIEMSG_REG(i));
224 void c67x00_ll_hpi_enable_sofeop(struct c67x00_sie *sie)
226 hpi_set_bits(sie->dev, HPI_IRQ_ROUTING_REG,
227 SOFEOP_TO_HPI_EN(sie->sie_num));
230 void c67x00_ll_hpi_disable_sofeop(struct c67x00_sie *sie)
232 hpi_clear_bits(sie->dev, HPI_IRQ_ROUTING_REG,
233 SOFEOP_TO_HPI_EN(sie->sie_num));
236 /* -------------------------------------------------------------------------- */
237 /* Transactions */
239 static inline u16 ll_recv_msg(struct c67x00_device *dev)
241 u16 res;
243 res = wait_for_completion_timeout(&dev->hpi.lcp.msg_received, 5 * HZ);
244 WARN_ON(!res);
246 return (res == 0) ? -EIO : 0;
249 /* -------------------------------------------------------------------------- */
250 /* General functions */
252 u16 c67x00_ll_fetch_siemsg(struct c67x00_device *dev, int sie_num)
254 u16 val;
256 val = hpi_read_word(dev, SIEMSG_REG(sie_num));
257 /* clear register to allow next message */
258 hpi_write_word(dev, SIEMSG_REG(sie_num), 0);
260 return val;
263 u16 c67x00_ll_get_usb_ctl(struct c67x00_sie *sie)
265 return hpi_read_word(sie->dev, USB_CTL_REG(sie->sie_num));
269 * c67x00_ll_usb_clear_status - clear the USB status bits
271 void c67x00_ll_usb_clear_status(struct c67x00_sie *sie, u16 bits)
273 hpi_write_word(sie->dev, USB_STAT_REG(sie->sie_num), bits);
276 u16 c67x00_ll_usb_get_status(struct c67x00_sie *sie)
278 return hpi_read_word(sie->dev, USB_STAT_REG(sie->sie_num));
281 /* -------------------------------------------------------------------------- */
283 static int c67x00_comm_exec_int(struct c67x00_device *dev, u16 nr,
284 struct c67x00_lcp_int_data *data)
286 int i, rc;
288 mutex_lock(&dev->hpi.lcp.mutex);
289 hpi_write_word(dev, COMM_INT_NUM, nr);
290 for (i = 0; i < COMM_REGS; i++)
291 hpi_write_word(dev, COMM_R(i), data->regs[i]);
292 hpi_send_mbox(dev, COMM_EXEC_INT);
293 rc = ll_recv_msg(dev);
294 mutex_unlock(&dev->hpi.lcp.mutex);
296 return rc;
299 /* -------------------------------------------------------------------------- */
301 void c67x00_ll_irq(struct c67x00_device *dev, u16 int_status)
303 if ((int_status & MBX_OUT_FLG) == 0)
304 return;
306 dev->hpi.lcp.last_msg = hpi_recv_mbox(dev);
307 complete(&dev->hpi.lcp.msg_received);
310 /* -------------------------------------------------------------------------- */
312 int c67x00_ll_reset(struct c67x00_device *dev)
314 int rc;
316 mutex_lock(&dev->hpi.lcp.mutex);
317 hpi_send_mbox(dev, COMM_RESET);
318 rc = ll_recv_msg(dev);
319 mutex_unlock(&dev->hpi.lcp.mutex);
321 return rc;
324 /* -------------------------------------------------------------------------- */
327 * c67x00_ll_write_mem_le16 - write into c67x00 memory
328 * Only data is little endian, addr has cpu endianess.
330 void c67x00_ll_write_mem_le16(struct c67x00_device *dev, u16 addr,
331 void *data, int len)
333 u8 *buf = data;
335 /* Sanity check */
336 if (addr + len > 0xffff) {
337 dev_err(&dev->pdev->dev,
338 "Trying to write beyond writable region!\n");
339 return;
342 if (addr & 0x01) {
343 /* unaligned access */
344 u16 tmp;
345 tmp = hpi_read_word(dev, addr - 1);
346 tmp = (tmp & 0x00ff) | (*buf++ << 8);
347 hpi_write_word(dev, addr - 1, tmp);
348 addr++;
349 len--;
352 hpi_write_words_le16(dev, addr, (u16 *)buf, len / 2);
353 buf += len & ~0x01;
354 addr += len & ~0x01;
355 len &= 0x01;
357 if (len) {
358 u16 tmp;
359 tmp = hpi_read_word(dev, addr);
360 tmp = (tmp & 0xff00) | *buf;
361 hpi_write_word(dev, addr, tmp);
366 * c67x00_ll_read_mem_le16 - read from c67x00 memory
367 * Only data is little endian, addr has cpu endianess.
369 void c67x00_ll_read_mem_le16(struct c67x00_device *dev, u16 addr,
370 void *data, int len)
372 u8 *buf = data;
374 if (addr & 0x01) {
375 /* unaligned access */
376 u16 tmp;
377 tmp = hpi_read_word(dev, addr - 1);
378 *buf++ = (tmp >> 8) & 0x00ff;
379 addr++;
380 len--;
383 hpi_read_words_le16(dev, addr, (u16 *)buf, len / 2);
384 buf += len & ~0x01;
385 addr += len & ~0x01;
386 len &= 0x01;
388 if (len) {
389 u16 tmp;
390 tmp = hpi_read_word(dev, addr);
391 *buf = tmp & 0x00ff;
395 /* -------------------------------------------------------------------------- */
397 void c67x00_ll_init(struct c67x00_device *dev)
399 mutex_init(&dev->hpi.lcp.mutex);
400 init_completion(&dev->hpi.lcp.msg_received);
403 void c67x00_ll_release(struct c67x00_device *dev)