c67x00-ll-hpi.c 12 KB

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