c67x00-ll-hpi.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. /*
  2. * c67x00-ll-hpi.c: Cypress C67X00 USB Low level interface using HPI
  3. *
  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.
  7. *
  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.
  12. *
  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.
  17. *
  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.
  22. */
  23. #include <asm/byteorder.h>
  24. #include <linux/delay.h>
  25. #include <linux/io.h>
  26. #include <linux/jiffies.h>
  27. #include <linux/usb/c67x00.h>
  28. #include "c67x00.h"
  29. #define COMM_REGS 14
  30. struct c67x00_lcp_int_data {
  31. u16 regs[COMM_REGS];
  32. };
  33. /* -------------------------------------------------------------------------- */
  34. /* Interface definitions */
  35. #define COMM_ACK 0x0FED
  36. #define COMM_NAK 0xDEAD
  37. #define COMM_RESET 0xFA50
  38. #define COMM_EXEC_INT 0xCE01
  39. #define COMM_INT_NUM 0x01C2
  40. /* Registers 0 to COMM_REGS-1 */
  41. #define COMM_R(x) (0x01C4 + 2 * (x))
  42. #define HUSB_SIE_pCurrentTDPtr(x) ((x) ? 0x01B2 : 0x01B0)
  43. #define HUSB_SIE_pTDListDone_Sem(x) ((x) ? 0x01B8 : 0x01B6)
  44. #define HUSB_pEOT 0x01B4
  45. /* Software interrupts */
  46. /* 114, 115: */
  47. #define HUSB_SIE_INIT_INT(x) ((x) ? 0x0073 : 0x0072)
  48. #define HUSB_RESET_INT 0x0074
  49. #define SUSB_INIT_INT 0x0071
  50. #define SUSB_INIT_INT_LOC (SUSB_INIT_INT * 2)
  51. /* -----------------------------------------------------------------------
  52. * HPI implementation
  53. *
  54. * The c67x00 chip also support control via SPI or HSS serial
  55. * interfaces. However, this driver assumes that register access can
  56. * be performed from IRQ context. While this is a safe assumption with
  57. * the HPI interface, it is not true for the serial interfaces.
  58. */
  59. /* HPI registers */
  60. #define HPI_DATA 0
  61. #define HPI_MAILBOX 1
  62. #define HPI_ADDR 2
  63. #define HPI_STATUS 3
  64. /*
  65. * According to CY7C67300 specification (tables 140 and 141) HPI read and
  66. * write cycle duration Tcyc must be at least 6T long, where T is 1/48MHz,
  67. * which is 125ns.
  68. */
  69. #define HPI_T_CYC_NS 125
  70. static inline u16 hpi_read_reg(struct c67x00_device *dev, int reg)
  71. {
  72. ndelay(HPI_T_CYC_NS);
  73. return __raw_readw(dev->hpi.base + reg * dev->hpi.regstep);
  74. }
  75. static inline void hpi_write_reg(struct c67x00_device *dev, int reg, u16 value)
  76. {
  77. ndelay(HPI_T_CYC_NS);
  78. __raw_writew(value, dev->hpi.base + reg * dev->hpi.regstep);
  79. }
  80. static inline u16 hpi_read_word_nolock(struct c67x00_device *dev, u16 reg)
  81. {
  82. hpi_write_reg(dev, HPI_ADDR, reg);
  83. return hpi_read_reg(dev, HPI_DATA);
  84. }
  85. static u16 hpi_read_word(struct c67x00_device *dev, u16 reg)
  86. {
  87. u16 value;
  88. unsigned long flags;
  89. spin_lock_irqsave(&dev->hpi.lock, flags);
  90. value = hpi_read_word_nolock(dev, reg);
  91. spin_unlock_irqrestore(&dev->hpi.lock, flags);
  92. return value;
  93. }
  94. static void hpi_write_word_nolock(struct c67x00_device *dev, u16 reg, u16 value)
  95. {
  96. hpi_write_reg(dev, HPI_ADDR, reg);
  97. hpi_write_reg(dev, HPI_DATA, value);
  98. }
  99. static void hpi_write_word(struct c67x00_device *dev, u16 reg, u16 value)
  100. {
  101. unsigned long flags;
  102. spin_lock_irqsave(&dev->hpi.lock, flags);
  103. hpi_write_word_nolock(dev, reg, value);
  104. spin_unlock_irqrestore(&dev->hpi.lock, flags);
  105. }
  106. /*
  107. * Only data is little endian, addr has cpu endianess
  108. */
  109. static void hpi_write_words_le16(struct c67x00_device *dev, u16 addr,
  110. __le16 *data, u16 count)
  111. {
  112. unsigned long flags;
  113. int i;
  114. spin_lock_irqsave(&dev->hpi.lock, flags);
  115. hpi_write_reg(dev, HPI_ADDR, addr);
  116. for (i = 0; i < count; i++)
  117. hpi_write_reg(dev, HPI_DATA, le16_to_cpu(*data++));
  118. spin_unlock_irqrestore(&dev->hpi.lock, flags);
  119. }
  120. /*
  121. * Only data is little endian, addr has cpu endianess
  122. */
  123. static void hpi_read_words_le16(struct c67x00_device *dev, u16 addr,
  124. __le16 *data, u16 count)
  125. {
  126. unsigned long flags;
  127. int i;
  128. spin_lock_irqsave(&dev->hpi.lock, flags);
  129. hpi_write_reg(dev, HPI_ADDR, addr);
  130. for (i = 0; i < count; i++)
  131. *data++ = cpu_to_le16(hpi_read_reg(dev, HPI_DATA));
  132. spin_unlock_irqrestore(&dev->hpi.lock, flags);
  133. }
  134. static void hpi_set_bits(struct c67x00_device *dev, u16 reg, u16 mask)
  135. {
  136. u16 value;
  137. unsigned long flags;
  138. spin_lock_irqsave(&dev->hpi.lock, flags);
  139. value = hpi_read_word_nolock(dev, reg);
  140. hpi_write_word_nolock(dev, reg, value | mask);
  141. spin_unlock_irqrestore(&dev->hpi.lock, flags);
  142. }
  143. static void hpi_clear_bits(struct c67x00_device *dev, u16 reg, u16 mask)
  144. {
  145. u16 value;
  146. unsigned long flags;
  147. spin_lock_irqsave(&dev->hpi.lock, flags);
  148. value = hpi_read_word_nolock(dev, reg);
  149. hpi_write_word_nolock(dev, reg, value & ~mask);
  150. spin_unlock_irqrestore(&dev->hpi.lock, flags);
  151. }
  152. static u16 hpi_recv_mbox(struct c67x00_device *dev)
  153. {
  154. u16 value;
  155. unsigned long flags;
  156. spin_lock_irqsave(&dev->hpi.lock, flags);
  157. value = hpi_read_reg(dev, HPI_MAILBOX);
  158. spin_unlock_irqrestore(&dev->hpi.lock, flags);
  159. return value;
  160. }
  161. static u16 hpi_send_mbox(struct c67x00_device *dev, u16 value)
  162. {
  163. unsigned long flags;
  164. spin_lock_irqsave(&dev->hpi.lock, flags);
  165. hpi_write_reg(dev, HPI_MAILBOX, value);
  166. spin_unlock_irqrestore(&dev->hpi.lock, flags);
  167. return value;
  168. }
  169. u16 c67x00_ll_hpi_status(struct c67x00_device *dev)
  170. {
  171. u16 value;
  172. unsigned long flags;
  173. spin_lock_irqsave(&dev->hpi.lock, flags);
  174. value = hpi_read_reg(dev, HPI_STATUS);
  175. spin_unlock_irqrestore(&dev->hpi.lock, flags);
  176. return value;
  177. }
  178. void c67x00_ll_hpi_reg_init(struct c67x00_device *dev)
  179. {
  180. int i;
  181. hpi_recv_mbox(dev);
  182. c67x00_ll_hpi_status(dev);
  183. hpi_write_word(dev, HPI_IRQ_ROUTING_REG, 0);
  184. for (i = 0; i < C67X00_SIES; i++) {
  185. hpi_write_word(dev, SIEMSG_REG(i), 0);
  186. hpi_read_word(dev, SIEMSG_REG(i));
  187. }
  188. }
  189. void c67x00_ll_hpi_enable_sofeop(struct c67x00_sie *sie)
  190. {
  191. hpi_set_bits(sie->dev, HPI_IRQ_ROUTING_REG,
  192. SOFEOP_TO_HPI_EN(sie->sie_num));
  193. }
  194. void c67x00_ll_hpi_disable_sofeop(struct c67x00_sie *sie)
  195. {
  196. hpi_clear_bits(sie->dev, HPI_IRQ_ROUTING_REG,
  197. SOFEOP_TO_HPI_EN(sie->sie_num));
  198. }
  199. /* -------------------------------------------------------------------------- */
  200. /* Transactions */
  201. static inline int ll_recv_msg(struct c67x00_device *dev)
  202. {
  203. u16 res;
  204. res = wait_for_completion_timeout(&dev->hpi.lcp.msg_received, 5 * HZ);
  205. WARN_ON(!res);
  206. return (res == 0) ? -EIO : 0;
  207. }
  208. /* -------------------------------------------------------------------------- */
  209. /* General functions */
  210. u16 c67x00_ll_fetch_siemsg(struct c67x00_device *dev, int sie_num)
  211. {
  212. u16 val;
  213. val = hpi_read_word(dev, SIEMSG_REG(sie_num));
  214. /* clear register to allow next message */
  215. hpi_write_word(dev, SIEMSG_REG(sie_num), 0);
  216. return val;
  217. }
  218. u16 c67x00_ll_get_usb_ctl(struct c67x00_sie *sie)
  219. {
  220. return hpi_read_word(sie->dev, USB_CTL_REG(sie->sie_num));
  221. }
  222. /**
  223. * c67x00_ll_usb_clear_status - clear the USB status bits
  224. */
  225. void c67x00_ll_usb_clear_status(struct c67x00_sie *sie, u16 bits)
  226. {
  227. hpi_write_word(sie->dev, USB_STAT_REG(sie->sie_num), bits);
  228. }
  229. u16 c67x00_ll_usb_get_status(struct c67x00_sie *sie)
  230. {
  231. return hpi_read_word(sie->dev, USB_STAT_REG(sie->sie_num));
  232. }
  233. /* -------------------------------------------------------------------------- */
  234. static int c67x00_comm_exec_int(struct c67x00_device *dev, u16 nr,
  235. struct c67x00_lcp_int_data *data)
  236. {
  237. int i, rc;
  238. mutex_lock(&dev->hpi.lcp.mutex);
  239. hpi_write_word(dev, COMM_INT_NUM, nr);
  240. for (i = 0; i < COMM_REGS; i++)
  241. hpi_write_word(dev, COMM_R(i), data->regs[i]);
  242. hpi_send_mbox(dev, COMM_EXEC_INT);
  243. rc = ll_recv_msg(dev);
  244. mutex_unlock(&dev->hpi.lcp.mutex);
  245. return rc;
  246. }
  247. /* -------------------------------------------------------------------------- */
  248. /* Host specific functions */
  249. void c67x00_ll_set_husb_eot(struct c67x00_device *dev, u16 value)
  250. {
  251. mutex_lock(&dev->hpi.lcp.mutex);
  252. hpi_write_word(dev, HUSB_pEOT, value);
  253. mutex_unlock(&dev->hpi.lcp.mutex);
  254. }
  255. static inline void c67x00_ll_husb_sie_init(struct c67x00_sie *sie)
  256. {
  257. struct c67x00_device *dev = sie->dev;
  258. struct c67x00_lcp_int_data data;
  259. int rc;
  260. rc = c67x00_comm_exec_int(dev, HUSB_SIE_INIT_INT(sie->sie_num), &data);
  261. BUG_ON(rc); /* No return path for error code; crash spectacularly */
  262. }
  263. void c67x00_ll_husb_reset(struct c67x00_sie *sie, int port)
  264. {
  265. struct c67x00_device *dev = sie->dev;
  266. struct c67x00_lcp_int_data data;
  267. int rc;
  268. data.regs[0] = 50; /* Reset USB port for 50ms */
  269. data.regs[1] = port | (sie->sie_num << 1);
  270. rc = c67x00_comm_exec_int(dev, HUSB_RESET_INT, &data);
  271. BUG_ON(rc); /* No return path for error code; crash spectacularly */
  272. }
  273. void c67x00_ll_husb_set_current_td(struct c67x00_sie *sie, u16 addr)
  274. {
  275. hpi_write_word(sie->dev, HUSB_SIE_pCurrentTDPtr(sie->sie_num), addr);
  276. }
  277. u16 c67x00_ll_husb_get_current_td(struct c67x00_sie *sie)
  278. {
  279. return hpi_read_word(sie->dev, HUSB_SIE_pCurrentTDPtr(sie->sie_num));
  280. }
  281. u16 c67x00_ll_husb_get_frame(struct c67x00_sie *sie)
  282. {
  283. return hpi_read_word(sie->dev, HOST_FRAME_REG(sie->sie_num));
  284. }
  285. void c67x00_ll_husb_init_host_port(struct c67x00_sie *sie)
  286. {
  287. /* Set port into host mode */
  288. hpi_set_bits(sie->dev, USB_CTL_REG(sie->sie_num), HOST_MODE);
  289. c67x00_ll_husb_sie_init(sie);
  290. /* Clear interrupts */
  291. c67x00_ll_usb_clear_status(sie, HOST_STAT_MASK);
  292. /* Check */
  293. if (!(hpi_read_word(sie->dev, USB_CTL_REG(sie->sie_num)) & HOST_MODE))
  294. dev_warn(sie_dev(sie),
  295. "SIE %d not set to host mode\n", sie->sie_num);
  296. }
  297. void c67x00_ll_husb_reset_port(struct c67x00_sie *sie, int port)
  298. {
  299. /* Clear connect change */
  300. c67x00_ll_usb_clear_status(sie, PORT_CONNECT_CHANGE(port));
  301. /* Enable interrupts */
  302. hpi_set_bits(sie->dev, HPI_IRQ_ROUTING_REG,
  303. SOFEOP_TO_CPU_EN(sie->sie_num));
  304. hpi_set_bits(sie->dev, HOST_IRQ_EN_REG(sie->sie_num),
  305. SOF_EOP_IRQ_EN | DONE_IRQ_EN);
  306. /* Enable pull down transistors */
  307. hpi_set_bits(sie->dev, USB_CTL_REG(sie->sie_num), PORT_RES_EN(port));
  308. }
  309. /* -------------------------------------------------------------------------- */
  310. void c67x00_ll_irq(struct c67x00_device *dev, u16 int_status)
  311. {
  312. if ((int_status & MBX_OUT_FLG) == 0)
  313. return;
  314. dev->hpi.lcp.last_msg = hpi_recv_mbox(dev);
  315. complete(&dev->hpi.lcp.msg_received);
  316. }
  317. /* -------------------------------------------------------------------------- */
  318. int c67x00_ll_reset(struct c67x00_device *dev)
  319. {
  320. int rc;
  321. mutex_lock(&dev->hpi.lcp.mutex);
  322. hpi_send_mbox(dev, COMM_RESET);
  323. rc = ll_recv_msg(dev);
  324. mutex_unlock(&dev->hpi.lcp.mutex);
  325. return rc;
  326. }
  327. /* -------------------------------------------------------------------------- */
  328. /**
  329. * c67x00_ll_write_mem_le16 - write into c67x00 memory
  330. * Only data is little endian, addr has cpu endianess.
  331. */
  332. void c67x00_ll_write_mem_le16(struct c67x00_device *dev, u16 addr,
  333. void *data, int len)
  334. {
  335. u8 *buf = data;
  336. /* Sanity check */
  337. if (addr + len > 0xffff) {
  338. dev_err(&dev->pdev->dev,
  339. "Trying to write beyond writable region!\n");
  340. return;
  341. }
  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--;
  350. }
  351. hpi_write_words_le16(dev, addr, (__le16 *)buf, len / 2);
  352. buf += len & ~0x01;
  353. addr += len & ~0x01;
  354. len &= 0x01;
  355. if (len) {
  356. u16 tmp;
  357. tmp = hpi_read_word(dev, addr);
  358. tmp = (tmp & 0xff00) | *buf;
  359. hpi_write_word(dev, addr, tmp);
  360. }
  361. }
  362. /**
  363. * c67x00_ll_read_mem_le16 - read from c67x00 memory
  364. * Only data is little endian, addr has cpu endianess.
  365. */
  366. void c67x00_ll_read_mem_le16(struct c67x00_device *dev, u16 addr,
  367. void *data, int len)
  368. {
  369. u8 *buf = data;
  370. if (addr & 0x01) {
  371. /* unaligned access */
  372. u16 tmp;
  373. tmp = hpi_read_word(dev, addr - 1);
  374. *buf++ = (tmp >> 8) & 0x00ff;
  375. addr++;
  376. len--;
  377. }
  378. hpi_read_words_le16(dev, addr, (__le16 *)buf, len / 2);
  379. buf += len & ~0x01;
  380. addr += len & ~0x01;
  381. len &= 0x01;
  382. if (len) {
  383. u16 tmp;
  384. tmp = hpi_read_word(dev, addr);
  385. *buf = tmp & 0x00ff;
  386. }
  387. }
  388. /* -------------------------------------------------------------------------- */
  389. void c67x00_ll_init(struct c67x00_device *dev)
  390. {
  391. mutex_init(&dev->hpi.lcp.mutex);
  392. init_completion(&dev->hpi.lcp.msg_received);
  393. }
  394. void c67x00_ll_release(struct c67x00_device *dev)
  395. {
  396. }