c67x00-ll-hpi.c 12 KB

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