c67x00-ll-hpi.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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. void c67x00_ll_irq(struct c67x00_device *dev, u16 int_status)
  239. {
  240. if ((int_status & MBX_OUT_FLG) == 0)
  241. return;
  242. dev->hpi.lcp.last_msg = hpi_recv_mbox(dev);
  243. complete(&dev->hpi.lcp.msg_received);
  244. }
  245. /* -------------------------------------------------------------------------- */
  246. int c67x00_ll_reset(struct c67x00_device *dev)
  247. {
  248. int rc;
  249. mutex_lock(&dev->hpi.lcp.mutex);
  250. hpi_send_mbox(dev, COMM_RESET);
  251. rc = ll_recv_msg(dev);
  252. mutex_unlock(&dev->hpi.lcp.mutex);
  253. return rc;
  254. }
  255. /* -------------------------------------------------------------------------- */
  256. /**
  257. * c67x00_ll_write_mem_le16 - write into c67x00 memory
  258. * Only data is little endian, addr has cpu endianess.
  259. */
  260. void c67x00_ll_write_mem_le16(struct c67x00_device *dev, u16 addr,
  261. void *data, int len)
  262. {
  263. u8 *buf = data;
  264. /* Sanity check */
  265. if (addr + len > 0xffff) {
  266. dev_err(&dev->pdev->dev,
  267. "Trying to write beyond writable region!\n");
  268. return;
  269. }
  270. if (addr & 0x01) {
  271. /* unaligned access */
  272. u16 tmp;
  273. tmp = hpi_read_word(dev, addr - 1);
  274. tmp = (tmp & 0x00ff) | (*buf++ << 8);
  275. hpi_write_word(dev, addr - 1, tmp);
  276. addr++;
  277. len--;
  278. }
  279. hpi_write_words_le16(dev, addr, (u16 *)buf, len / 2);
  280. buf += len & ~0x01;
  281. addr += len & ~0x01;
  282. len &= 0x01;
  283. if (len) {
  284. u16 tmp;
  285. tmp = hpi_read_word(dev, addr);
  286. tmp = (tmp & 0xff00) | *buf;
  287. hpi_write_word(dev, addr, tmp);
  288. }
  289. }
  290. /**
  291. * c67x00_ll_read_mem_le16 - read from c67x00 memory
  292. * Only data is little endian, addr has cpu endianess.
  293. */
  294. void c67x00_ll_read_mem_le16(struct c67x00_device *dev, u16 addr,
  295. void *data, int len)
  296. {
  297. u8 *buf = data;
  298. if (addr & 0x01) {
  299. /* unaligned access */
  300. u16 tmp;
  301. tmp = hpi_read_word(dev, addr - 1);
  302. *buf++ = (tmp >> 8) & 0x00ff;
  303. addr++;
  304. len--;
  305. }
  306. hpi_read_words_le16(dev, addr, (u16 *)buf, len / 2);
  307. buf += len & ~0x01;
  308. addr += len & ~0x01;
  309. len &= 0x01;
  310. if (len) {
  311. u16 tmp;
  312. tmp = hpi_read_word(dev, addr);
  313. *buf = tmp & 0x00ff;
  314. }
  315. }
  316. /* -------------------------------------------------------------------------- */
  317. void c67x00_ll_init(struct c67x00_device *dev)
  318. {
  319. mutex_init(&dev->hpi.lcp.mutex);
  320. init_completion(&dev->hpi.lcp.msg_received);
  321. }
  322. void c67x00_ll_release(struct c67x00_device *dev)
  323. {
  324. }