spi-fsl-espi.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848
  1. /*
  2. * Freescale eSPI controller driver.
  3. *
  4. * Copyright 2010 Freescale Semiconductor, Inc.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation; either version 2 of the License, or (at your
  9. * option) any later version.
  10. */
  11. #include <linux/delay.h>
  12. #include <linux/err.h>
  13. #include <linux/fsl_devices.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/module.h>
  16. #include <linux/mm.h>
  17. #include <linux/of.h>
  18. #include <linux/of_address.h>
  19. #include <linux/of_irq.h>
  20. #include <linux/of_platform.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/spi/spi.h>
  23. #include <linux/pm_runtime.h>
  24. #include <sysdev/fsl_soc.h>
  25. /* eSPI Controller registers */
  26. #define ESPI_SPMODE 0x00 /* eSPI mode register */
  27. #define ESPI_SPIE 0x04 /* eSPI event register */
  28. #define ESPI_SPIM 0x08 /* eSPI mask register */
  29. #define ESPI_SPCOM 0x0c /* eSPI command register */
  30. #define ESPI_SPITF 0x10 /* eSPI transmit FIFO access register*/
  31. #define ESPI_SPIRF 0x14 /* eSPI receive FIFO access register*/
  32. #define ESPI_SPMODE0 0x20 /* eSPI cs0 mode register */
  33. #define ESPI_SPMODEx(x) (ESPI_SPMODE0 + (x) * 4)
  34. /* eSPI Controller mode register definitions */
  35. #define SPMODE_ENABLE BIT(31)
  36. #define SPMODE_LOOP BIT(30)
  37. #define SPMODE_TXTHR(x) ((x) << 8)
  38. #define SPMODE_RXTHR(x) ((x) << 0)
  39. /* eSPI Controller CS mode register definitions */
  40. #define CSMODE_CI_INACTIVEHIGH BIT(31)
  41. #define CSMODE_CP_BEGIN_EDGECLK BIT(30)
  42. #define CSMODE_REV BIT(29)
  43. #define CSMODE_DIV16 BIT(28)
  44. #define CSMODE_PM(x) ((x) << 24)
  45. #define CSMODE_POL_1 BIT(20)
  46. #define CSMODE_LEN(x) ((x) << 16)
  47. #define CSMODE_BEF(x) ((x) << 12)
  48. #define CSMODE_AFT(x) ((x) << 8)
  49. #define CSMODE_CG(x) ((x) << 3)
  50. #define FSL_ESPI_FIFO_SIZE 32
  51. #define FSL_ESPI_RXTHR 15
  52. /* Default mode/csmode for eSPI controller */
  53. #define SPMODE_INIT_VAL (SPMODE_TXTHR(4) | SPMODE_RXTHR(FSL_ESPI_RXTHR))
  54. #define CSMODE_INIT_VAL (CSMODE_POL_1 | CSMODE_BEF(0) \
  55. | CSMODE_AFT(0) | CSMODE_CG(1))
  56. /* SPIE register values */
  57. #define SPIE_RXCNT(reg) ((reg >> 24) & 0x3F)
  58. #define SPIE_TXCNT(reg) ((reg >> 16) & 0x3F)
  59. #define SPIE_TXE BIT(15) /* TX FIFO empty */
  60. #define SPIE_DON BIT(14) /* TX done */
  61. #define SPIE_RXT BIT(13) /* RX FIFO threshold */
  62. #define SPIE_RXF BIT(12) /* RX FIFO full */
  63. #define SPIE_TXT BIT(11) /* TX FIFO threshold*/
  64. #define SPIE_RNE BIT(9) /* RX FIFO not empty */
  65. #define SPIE_TNF BIT(8) /* TX FIFO not full */
  66. /* SPIM register values */
  67. #define SPIM_TXE BIT(15) /* TX FIFO empty */
  68. #define SPIM_DON BIT(14) /* TX done */
  69. #define SPIM_RXT BIT(13) /* RX FIFO threshold */
  70. #define SPIM_RXF BIT(12) /* RX FIFO full */
  71. #define SPIM_TXT BIT(11) /* TX FIFO threshold*/
  72. #define SPIM_RNE BIT(9) /* RX FIFO not empty */
  73. #define SPIM_TNF BIT(8) /* TX FIFO not full */
  74. /* SPCOM register values */
  75. #define SPCOM_CS(x) ((x) << 30)
  76. #define SPCOM_DO BIT(28) /* Dual output */
  77. #define SPCOM_TO BIT(27) /* TX only */
  78. #define SPCOM_RXSKIP(x) ((x) << 16)
  79. #define SPCOM_TRANLEN(x) ((x) << 0)
  80. #define SPCOM_TRANLEN_MAX 0x10000 /* Max transaction length */
  81. #define AUTOSUSPEND_TIMEOUT 2000
  82. struct fsl_espi {
  83. struct device *dev;
  84. void __iomem *reg_base;
  85. struct list_head *m_transfers;
  86. struct spi_transfer *tx_t;
  87. unsigned int tx_pos;
  88. bool tx_done;
  89. struct spi_transfer *rx_t;
  90. unsigned int rx_pos;
  91. bool rx_done;
  92. bool swab;
  93. unsigned int rxskip;
  94. spinlock_t lock;
  95. u32 spibrg; /* SPIBRG input clock */
  96. struct completion done;
  97. };
  98. struct fsl_espi_cs {
  99. u32 hw_mode;
  100. };
  101. static inline u32 fsl_espi_read_reg(struct fsl_espi *espi, int offset)
  102. {
  103. return ioread32be(espi->reg_base + offset);
  104. }
  105. static inline u16 fsl_espi_read_reg16(struct fsl_espi *espi, int offset)
  106. {
  107. return ioread16be(espi->reg_base + offset);
  108. }
  109. static inline u8 fsl_espi_read_reg8(struct fsl_espi *espi, int offset)
  110. {
  111. return ioread8(espi->reg_base + offset);
  112. }
  113. static inline void fsl_espi_write_reg(struct fsl_espi *espi, int offset,
  114. u32 val)
  115. {
  116. iowrite32be(val, espi->reg_base + offset);
  117. }
  118. static inline void fsl_espi_write_reg16(struct fsl_espi *espi, int offset,
  119. u16 val)
  120. {
  121. iowrite16be(val, espi->reg_base + offset);
  122. }
  123. static inline void fsl_espi_write_reg8(struct fsl_espi *espi, int offset,
  124. u8 val)
  125. {
  126. iowrite8(val, espi->reg_base + offset);
  127. }
  128. static int fsl_espi_check_message(struct spi_message *m)
  129. {
  130. struct fsl_espi *espi = spi_master_get_devdata(m->spi->master);
  131. struct spi_transfer *t, *first;
  132. if (m->frame_length > SPCOM_TRANLEN_MAX) {
  133. dev_err(espi->dev, "message too long, size is %u bytes\n",
  134. m->frame_length);
  135. return -EMSGSIZE;
  136. }
  137. first = list_first_entry(&m->transfers, struct spi_transfer,
  138. transfer_list);
  139. list_for_each_entry(t, &m->transfers, transfer_list) {
  140. if (first->bits_per_word != t->bits_per_word ||
  141. first->speed_hz != t->speed_hz) {
  142. dev_err(espi->dev, "bits_per_word/speed_hz should be the same for all transfers\n");
  143. return -EINVAL;
  144. }
  145. }
  146. /* ESPI supports MSB-first transfers for word size 8 / 16 only */
  147. if (!(m->spi->mode & SPI_LSB_FIRST) && first->bits_per_word != 8 &&
  148. first->bits_per_word != 16) {
  149. dev_err(espi->dev,
  150. "MSB-first transfer not supported for wordsize %u\n",
  151. first->bits_per_word);
  152. return -EINVAL;
  153. }
  154. return 0;
  155. }
  156. static unsigned int fsl_espi_check_rxskip_mode(struct spi_message *m)
  157. {
  158. struct spi_transfer *t;
  159. unsigned int i = 0, rxskip = 0;
  160. /*
  161. * prerequisites for ESPI rxskip mode:
  162. * - message has two transfers
  163. * - first transfer is a write and second is a read
  164. *
  165. * In addition the current low-level transfer mechanism requires
  166. * that the rxskip bytes fit into the TX FIFO. Else the transfer
  167. * would hang because after the first FSL_ESPI_FIFO_SIZE bytes
  168. * the TX FIFO isn't re-filled.
  169. */
  170. list_for_each_entry(t, &m->transfers, transfer_list) {
  171. if (i == 0) {
  172. if (!t->tx_buf || t->rx_buf ||
  173. t->len > FSL_ESPI_FIFO_SIZE)
  174. return 0;
  175. rxskip = t->len;
  176. } else if (i == 1) {
  177. if (t->tx_buf || !t->rx_buf)
  178. return 0;
  179. }
  180. i++;
  181. }
  182. return i == 2 ? rxskip : 0;
  183. }
  184. static void fsl_espi_fill_tx_fifo(struct fsl_espi *espi, u32 events)
  185. {
  186. u32 tx_fifo_avail;
  187. unsigned int tx_left;
  188. const void *tx_buf;
  189. /* if events is zero transfer has not started and tx fifo is empty */
  190. tx_fifo_avail = events ? SPIE_TXCNT(events) : FSL_ESPI_FIFO_SIZE;
  191. start:
  192. tx_left = espi->tx_t->len - espi->tx_pos;
  193. tx_buf = espi->tx_t->tx_buf;
  194. while (tx_fifo_avail >= min(4U, tx_left) && tx_left) {
  195. if (tx_left >= 4) {
  196. if (!tx_buf)
  197. fsl_espi_write_reg(espi, ESPI_SPITF, 0);
  198. else if (espi->swab)
  199. fsl_espi_write_reg(espi, ESPI_SPITF,
  200. swahb32p(tx_buf + espi->tx_pos));
  201. else
  202. fsl_espi_write_reg(espi, ESPI_SPITF,
  203. *(u32 *)(tx_buf + espi->tx_pos));
  204. espi->tx_pos += 4;
  205. tx_left -= 4;
  206. tx_fifo_avail -= 4;
  207. } else if (tx_left >= 2 && tx_buf && espi->swab) {
  208. fsl_espi_write_reg16(espi, ESPI_SPITF,
  209. swab16p(tx_buf + espi->tx_pos));
  210. espi->tx_pos += 2;
  211. tx_left -= 2;
  212. tx_fifo_avail -= 2;
  213. } else {
  214. if (!tx_buf)
  215. fsl_espi_write_reg8(espi, ESPI_SPITF, 0);
  216. else
  217. fsl_espi_write_reg8(espi, ESPI_SPITF,
  218. *(u8 *)(tx_buf + espi->tx_pos));
  219. espi->tx_pos += 1;
  220. tx_left -= 1;
  221. tx_fifo_avail -= 1;
  222. }
  223. }
  224. if (!tx_left) {
  225. /* Last transfer finished, in rxskip mode only one is needed */
  226. if (list_is_last(&espi->tx_t->transfer_list,
  227. espi->m_transfers) || espi->rxskip) {
  228. espi->tx_done = true;
  229. return;
  230. }
  231. espi->tx_t = list_next_entry(espi->tx_t, transfer_list);
  232. espi->tx_pos = 0;
  233. /* continue with next transfer if tx fifo is not full */
  234. if (tx_fifo_avail)
  235. goto start;
  236. }
  237. }
  238. static void fsl_espi_read_rx_fifo(struct fsl_espi *espi, u32 events)
  239. {
  240. u32 rx_fifo_avail = SPIE_RXCNT(events);
  241. unsigned int rx_left;
  242. void *rx_buf;
  243. start:
  244. rx_left = espi->rx_t->len - espi->rx_pos;
  245. rx_buf = espi->rx_t->rx_buf;
  246. while (rx_fifo_avail >= min(4U, rx_left) && rx_left) {
  247. if (rx_left >= 4) {
  248. u32 val = fsl_espi_read_reg(espi, ESPI_SPIRF);
  249. if (rx_buf && espi->swab)
  250. *(u32 *)(rx_buf + espi->rx_pos) = swahb32(val);
  251. else if (rx_buf)
  252. *(u32 *)(rx_buf + espi->rx_pos) = val;
  253. espi->rx_pos += 4;
  254. rx_left -= 4;
  255. rx_fifo_avail -= 4;
  256. } else if (rx_left >= 2 && rx_buf && espi->swab) {
  257. u16 val = fsl_espi_read_reg16(espi, ESPI_SPIRF);
  258. *(u16 *)(rx_buf + espi->rx_pos) = swab16(val);
  259. espi->rx_pos += 2;
  260. rx_left -= 2;
  261. rx_fifo_avail -= 2;
  262. } else {
  263. u8 val = fsl_espi_read_reg8(espi, ESPI_SPIRF);
  264. if (rx_buf)
  265. *(u8 *)(rx_buf + espi->rx_pos) = val;
  266. espi->rx_pos += 1;
  267. rx_left -= 1;
  268. rx_fifo_avail -= 1;
  269. }
  270. }
  271. if (!rx_left) {
  272. if (list_is_last(&espi->rx_t->transfer_list,
  273. espi->m_transfers)) {
  274. espi->rx_done = true;
  275. return;
  276. }
  277. espi->rx_t = list_next_entry(espi->rx_t, transfer_list);
  278. espi->rx_pos = 0;
  279. /* continue with next transfer if rx fifo is not empty */
  280. if (rx_fifo_avail)
  281. goto start;
  282. }
  283. }
  284. static void fsl_espi_setup_transfer(struct spi_device *spi,
  285. struct spi_transfer *t)
  286. {
  287. struct fsl_espi *espi = spi_master_get_devdata(spi->master);
  288. int bits_per_word = t ? t->bits_per_word : spi->bits_per_word;
  289. u32 pm, hz = t ? t->speed_hz : spi->max_speed_hz;
  290. struct fsl_espi_cs *cs = spi_get_ctldata(spi);
  291. u32 hw_mode_old = cs->hw_mode;
  292. /* mask out bits we are going to set */
  293. cs->hw_mode &= ~(CSMODE_LEN(0xF) | CSMODE_DIV16 | CSMODE_PM(0xF));
  294. cs->hw_mode |= CSMODE_LEN(bits_per_word - 1);
  295. pm = DIV_ROUND_UP(espi->spibrg, hz * 4) - 1;
  296. if (pm > 15) {
  297. cs->hw_mode |= CSMODE_DIV16;
  298. pm = DIV_ROUND_UP(espi->spibrg, hz * 16 * 4) - 1;
  299. }
  300. cs->hw_mode |= CSMODE_PM(pm);
  301. /* don't write the mode register if the mode doesn't change */
  302. if (cs->hw_mode != hw_mode_old)
  303. fsl_espi_write_reg(espi, ESPI_SPMODEx(spi->chip_select),
  304. cs->hw_mode);
  305. }
  306. static int fsl_espi_bufs(struct spi_device *spi, struct spi_transfer *t)
  307. {
  308. struct fsl_espi *espi = spi_master_get_devdata(spi->master);
  309. unsigned int rx_len = t->len;
  310. u32 mask, spcom;
  311. int ret;
  312. reinit_completion(&espi->done);
  313. /* Set SPCOM[CS] and SPCOM[TRANLEN] field */
  314. spcom = SPCOM_CS(spi->chip_select);
  315. spcom |= SPCOM_TRANLEN(t->len - 1);
  316. /* configure RXSKIP mode */
  317. if (espi->rxskip) {
  318. spcom |= SPCOM_RXSKIP(espi->rxskip);
  319. rx_len = t->len - espi->rxskip;
  320. if (t->rx_nbits == SPI_NBITS_DUAL)
  321. spcom |= SPCOM_DO;
  322. }
  323. fsl_espi_write_reg(espi, ESPI_SPCOM, spcom);
  324. /* enable interrupts */
  325. mask = SPIM_DON;
  326. if (rx_len > FSL_ESPI_FIFO_SIZE)
  327. mask |= SPIM_RXT;
  328. fsl_espi_write_reg(espi, ESPI_SPIM, mask);
  329. /* Prevent filling the fifo from getting interrupted */
  330. spin_lock_irq(&espi->lock);
  331. fsl_espi_fill_tx_fifo(espi, 0);
  332. spin_unlock_irq(&espi->lock);
  333. /* Won't hang up forever, SPI bus sometimes got lost interrupts... */
  334. ret = wait_for_completion_timeout(&espi->done, 2 * HZ);
  335. if (ret == 0)
  336. dev_err(espi->dev, "Transfer timed out!\n");
  337. /* disable rx ints */
  338. fsl_espi_write_reg(espi, ESPI_SPIM, 0);
  339. return ret == 0 ? -ETIMEDOUT : 0;
  340. }
  341. static int fsl_espi_trans(struct spi_message *m, struct spi_transfer *trans)
  342. {
  343. struct fsl_espi *espi = spi_master_get_devdata(m->spi->master);
  344. struct spi_device *spi = m->spi;
  345. int ret;
  346. /* In case of LSB-first and bits_per_word > 8 byte-swap all words */
  347. espi->swab = spi->mode & SPI_LSB_FIRST && trans->bits_per_word > 8;
  348. espi->m_transfers = &m->transfers;
  349. espi->tx_t = list_first_entry(&m->transfers, struct spi_transfer,
  350. transfer_list);
  351. espi->tx_pos = 0;
  352. espi->tx_done = false;
  353. espi->rx_t = list_first_entry(&m->transfers, struct spi_transfer,
  354. transfer_list);
  355. espi->rx_pos = 0;
  356. espi->rx_done = false;
  357. espi->rxskip = fsl_espi_check_rxskip_mode(m);
  358. if (trans->rx_nbits == SPI_NBITS_DUAL && !espi->rxskip) {
  359. dev_err(espi->dev, "Dual output mode requires RXSKIP mode!\n");
  360. return -EINVAL;
  361. }
  362. /* In RXSKIP mode skip first transfer for reads */
  363. if (espi->rxskip)
  364. espi->rx_t = list_next_entry(espi->rx_t, transfer_list);
  365. fsl_espi_setup_transfer(spi, trans);
  366. ret = fsl_espi_bufs(spi, trans);
  367. if (trans->delay_usecs)
  368. udelay(trans->delay_usecs);
  369. return ret;
  370. }
  371. static int fsl_espi_do_one_msg(struct spi_master *master,
  372. struct spi_message *m)
  373. {
  374. unsigned int delay_usecs = 0, rx_nbits = 0;
  375. struct spi_transfer *t, trans = {};
  376. int ret;
  377. ret = fsl_espi_check_message(m);
  378. if (ret)
  379. goto out;
  380. list_for_each_entry(t, &m->transfers, transfer_list) {
  381. if (t->delay_usecs > delay_usecs)
  382. delay_usecs = t->delay_usecs;
  383. if (t->rx_nbits > rx_nbits)
  384. rx_nbits = t->rx_nbits;
  385. }
  386. t = list_first_entry(&m->transfers, struct spi_transfer,
  387. transfer_list);
  388. trans.len = m->frame_length;
  389. trans.speed_hz = t->speed_hz;
  390. trans.bits_per_word = t->bits_per_word;
  391. trans.delay_usecs = delay_usecs;
  392. trans.rx_nbits = rx_nbits;
  393. if (trans.len)
  394. ret = fsl_espi_trans(m, &trans);
  395. m->actual_length = ret ? 0 : trans.len;
  396. out:
  397. if (m->status == -EINPROGRESS)
  398. m->status = ret;
  399. spi_finalize_current_message(master);
  400. return ret;
  401. }
  402. static int fsl_espi_setup(struct spi_device *spi)
  403. {
  404. struct fsl_espi *espi;
  405. u32 loop_mode;
  406. struct fsl_espi_cs *cs = spi_get_ctldata(spi);
  407. if (!cs) {
  408. cs = kzalloc(sizeof(*cs), GFP_KERNEL);
  409. if (!cs)
  410. return -ENOMEM;
  411. spi_set_ctldata(spi, cs);
  412. }
  413. espi = spi_master_get_devdata(spi->master);
  414. pm_runtime_get_sync(espi->dev);
  415. cs->hw_mode = fsl_espi_read_reg(espi, ESPI_SPMODEx(spi->chip_select));
  416. /* mask out bits we are going to set */
  417. cs->hw_mode &= ~(CSMODE_CP_BEGIN_EDGECLK | CSMODE_CI_INACTIVEHIGH
  418. | CSMODE_REV);
  419. if (spi->mode & SPI_CPHA)
  420. cs->hw_mode |= CSMODE_CP_BEGIN_EDGECLK;
  421. if (spi->mode & SPI_CPOL)
  422. cs->hw_mode |= CSMODE_CI_INACTIVEHIGH;
  423. if (!(spi->mode & SPI_LSB_FIRST))
  424. cs->hw_mode |= CSMODE_REV;
  425. /* Handle the loop mode */
  426. loop_mode = fsl_espi_read_reg(espi, ESPI_SPMODE);
  427. loop_mode &= ~SPMODE_LOOP;
  428. if (spi->mode & SPI_LOOP)
  429. loop_mode |= SPMODE_LOOP;
  430. fsl_espi_write_reg(espi, ESPI_SPMODE, loop_mode);
  431. fsl_espi_setup_transfer(spi, NULL);
  432. pm_runtime_mark_last_busy(espi->dev);
  433. pm_runtime_put_autosuspend(espi->dev);
  434. return 0;
  435. }
  436. static void fsl_espi_cleanup(struct spi_device *spi)
  437. {
  438. struct fsl_espi_cs *cs = spi_get_ctldata(spi);
  439. kfree(cs);
  440. spi_set_ctldata(spi, NULL);
  441. }
  442. static void fsl_espi_cpu_irq(struct fsl_espi *espi, u32 events)
  443. {
  444. if (!espi->rx_done)
  445. fsl_espi_read_rx_fifo(espi, events);
  446. if (!espi->tx_done)
  447. fsl_espi_fill_tx_fifo(espi, events);
  448. if (!espi->tx_done || !espi->rx_done)
  449. return;
  450. /* we're done, but check for errors before returning */
  451. events = fsl_espi_read_reg(espi, ESPI_SPIE);
  452. if (!(events & SPIE_DON))
  453. dev_err(espi->dev,
  454. "Transfer done but SPIE_DON isn't set!\n");
  455. if (SPIE_RXCNT(events) || SPIE_TXCNT(events) != FSL_ESPI_FIFO_SIZE)
  456. dev_err(espi->dev, "Transfer done but rx/tx fifo's aren't empty!\n");
  457. complete(&espi->done);
  458. }
  459. static irqreturn_t fsl_espi_irq(s32 irq, void *context_data)
  460. {
  461. struct fsl_espi *espi = context_data;
  462. u32 events, mask;
  463. spin_lock(&espi->lock);
  464. /* Get interrupt events(tx/rx) */
  465. events = fsl_espi_read_reg(espi, ESPI_SPIE);
  466. mask = fsl_espi_read_reg(espi, ESPI_SPIM);
  467. if (!(events & mask)) {
  468. spin_unlock(&espi->lock);
  469. return IRQ_NONE;
  470. }
  471. dev_vdbg(espi->dev, "%s: events %x\n", __func__, events);
  472. fsl_espi_cpu_irq(espi, events);
  473. /* Clear the events */
  474. fsl_espi_write_reg(espi, ESPI_SPIE, events);
  475. spin_unlock(&espi->lock);
  476. return IRQ_HANDLED;
  477. }
  478. #ifdef CONFIG_PM
  479. static int fsl_espi_runtime_suspend(struct device *dev)
  480. {
  481. struct spi_master *master = dev_get_drvdata(dev);
  482. struct fsl_espi *espi = spi_master_get_devdata(master);
  483. u32 regval;
  484. regval = fsl_espi_read_reg(espi, ESPI_SPMODE);
  485. regval &= ~SPMODE_ENABLE;
  486. fsl_espi_write_reg(espi, ESPI_SPMODE, regval);
  487. return 0;
  488. }
  489. static int fsl_espi_runtime_resume(struct device *dev)
  490. {
  491. struct spi_master *master = dev_get_drvdata(dev);
  492. struct fsl_espi *espi = spi_master_get_devdata(master);
  493. u32 regval;
  494. regval = fsl_espi_read_reg(espi, ESPI_SPMODE);
  495. regval |= SPMODE_ENABLE;
  496. fsl_espi_write_reg(espi, ESPI_SPMODE, regval);
  497. return 0;
  498. }
  499. #endif
  500. static size_t fsl_espi_max_message_size(struct spi_device *spi)
  501. {
  502. return SPCOM_TRANLEN_MAX;
  503. }
  504. static void fsl_espi_init_regs(struct device *dev, bool initial)
  505. {
  506. struct spi_master *master = dev_get_drvdata(dev);
  507. struct fsl_espi *espi = spi_master_get_devdata(master);
  508. struct device_node *nc;
  509. u32 csmode, cs, prop;
  510. int ret;
  511. /* SPI controller initializations */
  512. fsl_espi_write_reg(espi, ESPI_SPMODE, 0);
  513. fsl_espi_write_reg(espi, ESPI_SPIM, 0);
  514. fsl_espi_write_reg(espi, ESPI_SPCOM, 0);
  515. fsl_espi_write_reg(espi, ESPI_SPIE, 0xffffffff);
  516. /* Init eSPI CS mode register */
  517. for_each_available_child_of_node(master->dev.of_node, nc) {
  518. /* get chip select */
  519. ret = of_property_read_u32(nc, "reg", &cs);
  520. if (ret || cs >= master->num_chipselect)
  521. continue;
  522. csmode = CSMODE_INIT_VAL;
  523. /* check if CSBEF is set in device tree */
  524. ret = of_property_read_u32(nc, "fsl,csbef", &prop);
  525. if (!ret) {
  526. csmode &= ~(CSMODE_BEF(0xf));
  527. csmode |= CSMODE_BEF(prop);
  528. }
  529. /* check if CSAFT is set in device tree */
  530. ret = of_property_read_u32(nc, "fsl,csaft", &prop);
  531. if (!ret) {
  532. csmode &= ~(CSMODE_AFT(0xf));
  533. csmode |= CSMODE_AFT(prop);
  534. }
  535. fsl_espi_write_reg(espi, ESPI_SPMODEx(cs), csmode);
  536. if (initial)
  537. dev_info(dev, "cs=%u, init_csmode=0x%x\n", cs, csmode);
  538. }
  539. /* Enable SPI interface */
  540. fsl_espi_write_reg(espi, ESPI_SPMODE, SPMODE_INIT_VAL | SPMODE_ENABLE);
  541. }
  542. static int fsl_espi_probe(struct device *dev, struct resource *mem,
  543. unsigned int irq, unsigned int num_cs)
  544. {
  545. struct spi_master *master;
  546. struct fsl_espi *espi;
  547. int ret;
  548. master = spi_alloc_master(dev, sizeof(struct fsl_espi));
  549. if (!master)
  550. return -ENOMEM;
  551. dev_set_drvdata(dev, master);
  552. master->mode_bits = SPI_RX_DUAL | SPI_CPOL | SPI_CPHA | SPI_CS_HIGH |
  553. SPI_LSB_FIRST | SPI_LOOP;
  554. master->dev.of_node = dev->of_node;
  555. master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 16);
  556. master->setup = fsl_espi_setup;
  557. master->cleanup = fsl_espi_cleanup;
  558. master->transfer_one_message = fsl_espi_do_one_msg;
  559. master->auto_runtime_pm = true;
  560. master->max_message_size = fsl_espi_max_message_size;
  561. master->num_chipselect = num_cs;
  562. espi = spi_master_get_devdata(master);
  563. spin_lock_init(&espi->lock);
  564. espi->dev = dev;
  565. espi->spibrg = fsl_get_sys_freq();
  566. if (espi->spibrg == -1) {
  567. dev_err(dev, "Can't get sys frequency!\n");
  568. ret = -EINVAL;
  569. goto err_probe;
  570. }
  571. /* determined by clock divider fields DIV16/PM in register SPMODEx */
  572. master->min_speed_hz = DIV_ROUND_UP(espi->spibrg, 4 * 16 * 16);
  573. master->max_speed_hz = DIV_ROUND_UP(espi->spibrg, 4);
  574. init_completion(&espi->done);
  575. espi->reg_base = devm_ioremap_resource(dev, mem);
  576. if (IS_ERR(espi->reg_base)) {
  577. ret = PTR_ERR(espi->reg_base);
  578. goto err_probe;
  579. }
  580. /* Register for SPI Interrupt */
  581. ret = devm_request_irq(dev, irq, fsl_espi_irq, 0, "fsl_espi", espi);
  582. if (ret)
  583. goto err_probe;
  584. fsl_espi_init_regs(dev, true);
  585. pm_runtime_set_autosuspend_delay(dev, AUTOSUSPEND_TIMEOUT);
  586. pm_runtime_use_autosuspend(dev);
  587. pm_runtime_set_active(dev);
  588. pm_runtime_enable(dev);
  589. pm_runtime_get_sync(dev);
  590. ret = devm_spi_register_master(dev, master);
  591. if (ret < 0)
  592. goto err_pm;
  593. dev_info(dev, "at 0x%p (irq = %u)\n", espi->reg_base, irq);
  594. pm_runtime_mark_last_busy(dev);
  595. pm_runtime_put_autosuspend(dev);
  596. return 0;
  597. err_pm:
  598. pm_runtime_put_noidle(dev);
  599. pm_runtime_disable(dev);
  600. pm_runtime_set_suspended(dev);
  601. err_probe:
  602. spi_master_put(master);
  603. return ret;
  604. }
  605. static int of_fsl_espi_get_chipselects(struct device *dev)
  606. {
  607. struct device_node *np = dev->of_node;
  608. u32 num_cs;
  609. int ret;
  610. ret = of_property_read_u32(np, "fsl,espi-num-chipselects", &num_cs);
  611. if (ret) {
  612. dev_err(dev, "No 'fsl,espi-num-chipselects' property\n");
  613. return 0;
  614. }
  615. return num_cs;
  616. }
  617. static int of_fsl_espi_probe(struct platform_device *ofdev)
  618. {
  619. struct device *dev = &ofdev->dev;
  620. struct device_node *np = ofdev->dev.of_node;
  621. struct resource mem;
  622. unsigned int irq, num_cs;
  623. int ret;
  624. if (of_property_read_bool(np, "mode")) {
  625. dev_err(dev, "mode property is not supported on ESPI!\n");
  626. return -EINVAL;
  627. }
  628. num_cs = of_fsl_espi_get_chipselects(dev);
  629. if (!num_cs)
  630. return -EINVAL;
  631. ret = of_address_to_resource(np, 0, &mem);
  632. if (ret)
  633. return ret;
  634. irq = irq_of_parse_and_map(np, 0);
  635. if (!irq)
  636. return -EINVAL;
  637. return fsl_espi_probe(dev, &mem, irq, num_cs);
  638. }
  639. static int of_fsl_espi_remove(struct platform_device *dev)
  640. {
  641. pm_runtime_disable(&dev->dev);
  642. return 0;
  643. }
  644. #ifdef CONFIG_PM_SLEEP
  645. static int of_fsl_espi_suspend(struct device *dev)
  646. {
  647. struct spi_master *master = dev_get_drvdata(dev);
  648. int ret;
  649. ret = spi_master_suspend(master);
  650. if (ret) {
  651. dev_warn(dev, "cannot suspend master\n");
  652. return ret;
  653. }
  654. return pm_runtime_force_suspend(dev);
  655. }
  656. static int of_fsl_espi_resume(struct device *dev)
  657. {
  658. struct spi_master *master = dev_get_drvdata(dev);
  659. int ret;
  660. fsl_espi_init_regs(dev, false);
  661. ret = pm_runtime_force_resume(dev);
  662. if (ret < 0)
  663. return ret;
  664. return spi_master_resume(master);
  665. }
  666. #endif /* CONFIG_PM_SLEEP */
  667. static const struct dev_pm_ops espi_pm = {
  668. SET_RUNTIME_PM_OPS(fsl_espi_runtime_suspend,
  669. fsl_espi_runtime_resume, NULL)
  670. SET_SYSTEM_SLEEP_PM_OPS(of_fsl_espi_suspend, of_fsl_espi_resume)
  671. };
  672. static const struct of_device_id of_fsl_espi_match[] = {
  673. { .compatible = "fsl,mpc8536-espi" },
  674. {}
  675. };
  676. MODULE_DEVICE_TABLE(of, of_fsl_espi_match);
  677. static struct platform_driver fsl_espi_driver = {
  678. .driver = {
  679. .name = "fsl_espi",
  680. .of_match_table = of_fsl_espi_match,
  681. .pm = &espi_pm,
  682. },
  683. .probe = of_fsl_espi_probe,
  684. .remove = of_fsl_espi_remove,
  685. };
  686. module_platform_driver(fsl_espi_driver);
  687. MODULE_AUTHOR("Mingkai Hu");
  688. MODULE_DESCRIPTION("Enhanced Freescale SPI Driver");
  689. MODULE_LICENSE("GPL");