dt-bindings: mtd: ingenic: Use standard ecc-engine property
[linux/fpc-iii.git] / drivers / usb / c67x00 / c67x00-ll-hpi.c
blobe1fe3603140a85fe4fd99bf8695cd4f3f069e62c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * c67x00-ll-hpi.c: Cypress C67X00 USB Low level interface using HPI
5 * Copyright (C) 2006-2008 Barco N.V.
6 * Derived from the Cypress cy7c67200/300 ezusb linux driver and
7 * based on multiple host controller drivers inside the linux kernel.
8 */
10 #include <asm/byteorder.h>
11 #include <linux/delay.h>
12 #include <linux/io.h>
13 #include <linux/jiffies.h>
14 #include <linux/usb/c67x00.h>
15 #include "c67x00.h"
17 #define COMM_REGS 14
19 struct c67x00_lcp_int_data {
20 u16 regs[COMM_REGS];
23 /* -------------------------------------------------------------------------- */
24 /* Interface definitions */
26 #define COMM_ACK 0x0FED
27 #define COMM_NAK 0xDEAD
29 #define COMM_RESET 0xFA50
30 #define COMM_EXEC_INT 0xCE01
31 #define COMM_INT_NUM 0x01C2
33 /* Registers 0 to COMM_REGS-1 */
34 #define COMM_R(x) (0x01C4 + 2 * (x))
36 #define HUSB_SIE_pCurrentTDPtr(x) ((x) ? 0x01B2 : 0x01B0)
37 #define HUSB_SIE_pTDListDone_Sem(x) ((x) ? 0x01B8 : 0x01B6)
38 #define HUSB_pEOT 0x01B4
40 /* Software interrupts */
41 /* 114, 115: */
42 #define HUSB_SIE_INIT_INT(x) ((x) ? 0x0073 : 0x0072)
43 #define HUSB_RESET_INT 0x0074
45 #define SUSB_INIT_INT 0x0071
46 #define SUSB_INIT_INT_LOC (SUSB_INIT_INT * 2)
48 /* -----------------------------------------------------------------------
49 * HPI implementation
51 * The c67x00 chip also support control via SPI or HSS serial
52 * interfaces. However, this driver assumes that register access can
53 * be performed from IRQ context. While this is a safe assumption with
54 * the HPI interface, it is not true for the serial interfaces.
57 /* HPI registers */
58 #define HPI_DATA 0
59 #define HPI_MAILBOX 1
60 #define HPI_ADDR 2
61 #define HPI_STATUS 3
64 * According to CY7C67300 specification (tables 140 and 141) HPI read and
65 * write cycle duration Tcyc must be at least 6T long, where T is 1/48MHz,
66 * which is 125ns.
68 #define HPI_T_CYC_NS 125
70 static inline u16 hpi_read_reg(struct c67x00_device *dev, int reg)
72 ndelay(HPI_T_CYC_NS);
73 return __raw_readw(dev->hpi.base + reg * dev->hpi.regstep);
76 static inline void hpi_write_reg(struct c67x00_device *dev, int reg, u16 value)
78 ndelay(HPI_T_CYC_NS);
79 __raw_writew(value, dev->hpi.base + reg * dev->hpi.regstep);
82 static inline u16 hpi_read_word_nolock(struct c67x00_device *dev, u16 reg)
84 hpi_write_reg(dev, HPI_ADDR, reg);
85 return hpi_read_reg(dev, HPI_DATA);
88 static u16 hpi_read_word(struct c67x00_device *dev, u16 reg)
90 u16 value;
91 unsigned long flags;
93 spin_lock_irqsave(&dev->hpi.lock, flags);
94 value = hpi_read_word_nolock(dev, reg);
95 spin_unlock_irqrestore(&dev->hpi.lock, flags);
97 return value;
100 static void hpi_write_word_nolock(struct c67x00_device *dev, u16 reg, u16 value)
102 hpi_write_reg(dev, HPI_ADDR, reg);
103 hpi_write_reg(dev, HPI_DATA, value);
106 static void hpi_write_word(struct c67x00_device *dev, u16 reg, u16 value)
108 unsigned long flags;
110 spin_lock_irqsave(&dev->hpi.lock, flags);
111 hpi_write_word_nolock(dev, reg, value);
112 spin_unlock_irqrestore(&dev->hpi.lock, flags);
116 * Only data is little endian, addr has cpu endianess
118 static void hpi_write_words_le16(struct c67x00_device *dev, u16 addr,
119 __le16 *data, u16 count)
121 unsigned long flags;
122 int i;
124 spin_lock_irqsave(&dev->hpi.lock, flags);
126 hpi_write_reg(dev, HPI_ADDR, addr);
127 for (i = 0; i < count; i++)
128 hpi_write_reg(dev, HPI_DATA, le16_to_cpu(*data++));
130 spin_unlock_irqrestore(&dev->hpi.lock, flags);
134 * Only data is little endian, addr has cpu endianess
136 static void hpi_read_words_le16(struct c67x00_device *dev, u16 addr,
137 __le16 *data, u16 count)
139 unsigned long flags;
140 int i;
142 spin_lock_irqsave(&dev->hpi.lock, flags);
143 hpi_write_reg(dev, HPI_ADDR, addr);
144 for (i = 0; i < count; i++)
145 *data++ = cpu_to_le16(hpi_read_reg(dev, HPI_DATA));
147 spin_unlock_irqrestore(&dev->hpi.lock, flags);
150 static void hpi_set_bits(struct c67x00_device *dev, u16 reg, u16 mask)
152 u16 value;
153 unsigned long flags;
155 spin_lock_irqsave(&dev->hpi.lock, flags);
156 value = hpi_read_word_nolock(dev, reg);
157 hpi_write_word_nolock(dev, reg, value | mask);
158 spin_unlock_irqrestore(&dev->hpi.lock, flags);
161 static void hpi_clear_bits(struct c67x00_device *dev, u16 reg, u16 mask)
163 u16 value;
164 unsigned long flags;
166 spin_lock_irqsave(&dev->hpi.lock, flags);
167 value = hpi_read_word_nolock(dev, reg);
168 hpi_write_word_nolock(dev, reg, value & ~mask);
169 spin_unlock_irqrestore(&dev->hpi.lock, flags);
172 static u16 hpi_recv_mbox(struct c67x00_device *dev)
174 u16 value;
175 unsigned long flags;
177 spin_lock_irqsave(&dev->hpi.lock, flags);
178 value = hpi_read_reg(dev, HPI_MAILBOX);
179 spin_unlock_irqrestore(&dev->hpi.lock, flags);
181 return value;
184 static u16 hpi_send_mbox(struct c67x00_device *dev, u16 value)
186 unsigned long flags;
188 spin_lock_irqsave(&dev->hpi.lock, flags);
189 hpi_write_reg(dev, HPI_MAILBOX, value);
190 spin_unlock_irqrestore(&dev->hpi.lock, flags);
192 return value;
195 u16 c67x00_ll_hpi_status(struct c67x00_device *dev)
197 u16 value;
198 unsigned long flags;
200 spin_lock_irqsave(&dev->hpi.lock, flags);
201 value = hpi_read_reg(dev, HPI_STATUS);
202 spin_unlock_irqrestore(&dev->hpi.lock, flags);
204 return value;
207 void c67x00_ll_hpi_reg_init(struct c67x00_device *dev)
209 int i;
211 hpi_recv_mbox(dev);
212 c67x00_ll_hpi_status(dev);
213 hpi_write_word(dev, HPI_IRQ_ROUTING_REG, 0);
215 for (i = 0; i < C67X00_SIES; i++) {
216 hpi_write_word(dev, SIEMSG_REG(i), 0);
217 hpi_read_word(dev, SIEMSG_REG(i));
221 void c67x00_ll_hpi_enable_sofeop(struct c67x00_sie *sie)
223 hpi_set_bits(sie->dev, HPI_IRQ_ROUTING_REG,
224 SOFEOP_TO_HPI_EN(sie->sie_num));
227 void c67x00_ll_hpi_disable_sofeop(struct c67x00_sie *sie)
229 hpi_clear_bits(sie->dev, HPI_IRQ_ROUTING_REG,
230 SOFEOP_TO_HPI_EN(sie->sie_num));
233 /* -------------------------------------------------------------------------- */
234 /* Transactions */
236 static inline int ll_recv_msg(struct c67x00_device *dev)
238 u16 res;
240 res = wait_for_completion_timeout(&dev->hpi.lcp.msg_received, 5 * HZ);
241 WARN_ON(!res);
243 return (res == 0) ? -EIO : 0;
246 /* -------------------------------------------------------------------------- */
247 /* General functions */
249 u16 c67x00_ll_fetch_siemsg(struct c67x00_device *dev, int sie_num)
251 u16 val;
253 val = hpi_read_word(dev, SIEMSG_REG(sie_num));
254 /* clear register to allow next message */
255 hpi_write_word(dev, SIEMSG_REG(sie_num), 0);
257 return val;
260 u16 c67x00_ll_get_usb_ctl(struct c67x00_sie *sie)
262 return hpi_read_word(sie->dev, USB_CTL_REG(sie->sie_num));
266 * c67x00_ll_usb_clear_status - clear the USB status bits
268 void c67x00_ll_usb_clear_status(struct c67x00_sie *sie, u16 bits)
270 hpi_write_word(sie->dev, USB_STAT_REG(sie->sie_num), bits);
273 u16 c67x00_ll_usb_get_status(struct c67x00_sie *sie)
275 return hpi_read_word(sie->dev, USB_STAT_REG(sie->sie_num));
278 /* -------------------------------------------------------------------------- */
280 static int c67x00_comm_exec_int(struct c67x00_device *dev, u16 nr,
281 struct c67x00_lcp_int_data *data)
283 int i, rc;
285 mutex_lock(&dev->hpi.lcp.mutex);
286 hpi_write_word(dev, COMM_INT_NUM, nr);
287 for (i = 0; i < COMM_REGS; i++)
288 hpi_write_word(dev, COMM_R(i), data->regs[i]);
289 hpi_send_mbox(dev, COMM_EXEC_INT);
290 rc = ll_recv_msg(dev);
291 mutex_unlock(&dev->hpi.lcp.mutex);
293 return rc;
296 /* -------------------------------------------------------------------------- */
297 /* Host specific functions */
299 void c67x00_ll_set_husb_eot(struct c67x00_device *dev, u16 value)
301 mutex_lock(&dev->hpi.lcp.mutex);
302 hpi_write_word(dev, HUSB_pEOT, value);
303 mutex_unlock(&dev->hpi.lcp.mutex);
306 static inline void c67x00_ll_husb_sie_init(struct c67x00_sie *sie)
308 struct c67x00_device *dev = sie->dev;
309 struct c67x00_lcp_int_data data;
310 int rc;
312 rc = c67x00_comm_exec_int(dev, HUSB_SIE_INIT_INT(sie->sie_num), &data);
313 BUG_ON(rc); /* No return path for error code; crash spectacularly */
316 void c67x00_ll_husb_reset(struct c67x00_sie *sie, int port)
318 struct c67x00_device *dev = sie->dev;
319 struct c67x00_lcp_int_data data;
320 int rc;
322 data.regs[0] = 50; /* Reset USB port for 50ms */
323 data.regs[1] = port | (sie->sie_num << 1);
324 rc = c67x00_comm_exec_int(dev, HUSB_RESET_INT, &data);
325 BUG_ON(rc); /* No return path for error code; crash spectacularly */
328 void c67x00_ll_husb_set_current_td(struct c67x00_sie *sie, u16 addr)
330 hpi_write_word(sie->dev, HUSB_SIE_pCurrentTDPtr(sie->sie_num), addr);
333 u16 c67x00_ll_husb_get_current_td(struct c67x00_sie *sie)
335 return hpi_read_word(sie->dev, HUSB_SIE_pCurrentTDPtr(sie->sie_num));
338 u16 c67x00_ll_husb_get_frame(struct c67x00_sie *sie)
340 return hpi_read_word(sie->dev, HOST_FRAME_REG(sie->sie_num));
343 void c67x00_ll_husb_init_host_port(struct c67x00_sie *sie)
345 /* Set port into host mode */
346 hpi_set_bits(sie->dev, USB_CTL_REG(sie->sie_num), HOST_MODE);
347 c67x00_ll_husb_sie_init(sie);
348 /* Clear interrupts */
349 c67x00_ll_usb_clear_status(sie, HOST_STAT_MASK);
350 /* Check */
351 if (!(hpi_read_word(sie->dev, USB_CTL_REG(sie->sie_num)) & HOST_MODE))
352 dev_warn(sie_dev(sie),
353 "SIE %d not set to host mode\n", sie->sie_num);
356 void c67x00_ll_husb_reset_port(struct c67x00_sie *sie, int port)
358 /* Clear connect change */
359 c67x00_ll_usb_clear_status(sie, PORT_CONNECT_CHANGE(port));
361 /* Enable interrupts */
362 hpi_set_bits(sie->dev, HPI_IRQ_ROUTING_REG,
363 SOFEOP_TO_CPU_EN(sie->sie_num));
364 hpi_set_bits(sie->dev, HOST_IRQ_EN_REG(sie->sie_num),
365 SOF_EOP_IRQ_EN | DONE_IRQ_EN);
367 /* Enable pull down transistors */
368 hpi_set_bits(sie->dev, USB_CTL_REG(sie->sie_num), PORT_RES_EN(port));
371 /* -------------------------------------------------------------------------- */
373 void c67x00_ll_irq(struct c67x00_device *dev, u16 int_status)
375 if ((int_status & MBX_OUT_FLG) == 0)
376 return;
378 dev->hpi.lcp.last_msg = hpi_recv_mbox(dev);
379 complete(&dev->hpi.lcp.msg_received);
382 /* -------------------------------------------------------------------------- */
384 int c67x00_ll_reset(struct c67x00_device *dev)
386 int rc;
388 mutex_lock(&dev->hpi.lcp.mutex);
389 hpi_send_mbox(dev, COMM_RESET);
390 rc = ll_recv_msg(dev);
391 mutex_unlock(&dev->hpi.lcp.mutex);
393 return rc;
396 /* -------------------------------------------------------------------------- */
399 * c67x00_ll_write_mem_le16 - write into c67x00 memory
400 * Only data is little endian, addr has cpu endianess.
402 void c67x00_ll_write_mem_le16(struct c67x00_device *dev, u16 addr,
403 void *data, int len)
405 u8 *buf = data;
407 /* Sanity check */
408 if (addr + len > 0xffff) {
409 dev_err(&dev->pdev->dev,
410 "Trying to write beyond writable region!\n");
411 return;
414 if (addr & 0x01) {
415 /* unaligned access */
416 u16 tmp;
417 tmp = hpi_read_word(dev, addr - 1);
418 tmp = (tmp & 0x00ff) | (*buf++ << 8);
419 hpi_write_word(dev, addr - 1, tmp);
420 addr++;
421 len--;
424 hpi_write_words_le16(dev, addr, (__le16 *)buf, len / 2);
425 buf += len & ~0x01;
426 addr += len & ~0x01;
427 len &= 0x01;
429 if (len) {
430 u16 tmp;
431 tmp = hpi_read_word(dev, addr);
432 tmp = (tmp & 0xff00) | *buf;
433 hpi_write_word(dev, addr, tmp);
438 * c67x00_ll_read_mem_le16 - read from c67x00 memory
439 * Only data is little endian, addr has cpu endianess.
441 void c67x00_ll_read_mem_le16(struct c67x00_device *dev, u16 addr,
442 void *data, int len)
444 u8 *buf = data;
446 if (addr & 0x01) {
447 /* unaligned access */
448 u16 tmp;
449 tmp = hpi_read_word(dev, addr - 1);
450 *buf++ = (tmp >> 8) & 0x00ff;
451 addr++;
452 len--;
455 hpi_read_words_le16(dev, addr, (__le16 *)buf, len / 2);
456 buf += len & ~0x01;
457 addr += len & ~0x01;
458 len &= 0x01;
460 if (len) {
461 u16 tmp;
462 tmp = hpi_read_word(dev, addr);
463 *buf = tmp & 0x00ff;
467 /* -------------------------------------------------------------------------- */
469 void c67x00_ll_init(struct c67x00_device *dev)
471 mutex_init(&dev->hpi.lcp.mutex);
472 init_completion(&dev->hpi.lcp.msg_received);
475 void c67x00_ll_release(struct c67x00_device *dev)