spi-fsl-espi.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850
  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. dev_err(espi->dev, "SPIE_RXCNT = %d, SPIE_TXCNT = %d\n",
  458. SPIE_RXCNT(events), SPIE_TXCNT(events));
  459. }
  460. complete(&espi->done);
  461. }
  462. static irqreturn_t fsl_espi_irq(s32 irq, void *context_data)
  463. {
  464. struct fsl_espi *espi = context_data;
  465. u32 events;
  466. spin_lock(&espi->lock);
  467. /* Get interrupt events(tx/rx) */
  468. events = fsl_espi_read_reg(espi, ESPI_SPIE);
  469. if (!events) {
  470. spin_unlock(&espi->lock);
  471. return IRQ_NONE;
  472. }
  473. dev_vdbg(espi->dev, "%s: events %x\n", __func__, events);
  474. fsl_espi_cpu_irq(espi, events);
  475. /* Clear the events */
  476. fsl_espi_write_reg(espi, ESPI_SPIE, events);
  477. spin_unlock(&espi->lock);
  478. return IRQ_HANDLED;
  479. }
  480. #ifdef CONFIG_PM
  481. static int fsl_espi_runtime_suspend(struct device *dev)
  482. {
  483. struct spi_master *master = dev_get_drvdata(dev);
  484. struct fsl_espi *espi = spi_master_get_devdata(master);
  485. u32 regval;
  486. regval = fsl_espi_read_reg(espi, ESPI_SPMODE);
  487. regval &= ~SPMODE_ENABLE;
  488. fsl_espi_write_reg(espi, ESPI_SPMODE, regval);
  489. return 0;
  490. }
  491. static int fsl_espi_runtime_resume(struct device *dev)
  492. {
  493. struct spi_master *master = dev_get_drvdata(dev);
  494. struct fsl_espi *espi = spi_master_get_devdata(master);
  495. u32 regval;
  496. regval = fsl_espi_read_reg(espi, ESPI_SPMODE);
  497. regval |= SPMODE_ENABLE;
  498. fsl_espi_write_reg(espi, ESPI_SPMODE, regval);
  499. return 0;
  500. }
  501. #endif
  502. static size_t fsl_espi_max_message_size(struct spi_device *spi)
  503. {
  504. return SPCOM_TRANLEN_MAX;
  505. }
  506. static void fsl_espi_init_regs(struct device *dev, bool initial)
  507. {
  508. struct spi_master *master = dev_get_drvdata(dev);
  509. struct fsl_espi *espi = spi_master_get_devdata(master);
  510. struct device_node *nc;
  511. u32 csmode, cs, prop;
  512. int ret;
  513. /* SPI controller initializations */
  514. fsl_espi_write_reg(espi, ESPI_SPMODE, 0);
  515. fsl_espi_write_reg(espi, ESPI_SPIM, 0);
  516. fsl_espi_write_reg(espi, ESPI_SPCOM, 0);
  517. fsl_espi_write_reg(espi, ESPI_SPIE, 0xffffffff);
  518. /* Init eSPI CS mode register */
  519. for_each_available_child_of_node(master->dev.of_node, nc) {
  520. /* get chip select */
  521. ret = of_property_read_u32(nc, "reg", &cs);
  522. if (ret || cs >= master->num_chipselect)
  523. continue;
  524. csmode = CSMODE_INIT_VAL;
  525. /* check if CSBEF is set in device tree */
  526. ret = of_property_read_u32(nc, "fsl,csbef", &prop);
  527. if (!ret) {
  528. csmode &= ~(CSMODE_BEF(0xf));
  529. csmode |= CSMODE_BEF(prop);
  530. }
  531. /* check if CSAFT is set in device tree */
  532. ret = of_property_read_u32(nc, "fsl,csaft", &prop);
  533. if (!ret) {
  534. csmode &= ~(CSMODE_AFT(0xf));
  535. csmode |= CSMODE_AFT(prop);
  536. }
  537. fsl_espi_write_reg(espi, ESPI_SPMODEx(cs), csmode);
  538. if (initial)
  539. dev_info(dev, "cs=%u, init_csmode=0x%x\n", cs, csmode);
  540. }
  541. /* Enable SPI interface */
  542. fsl_espi_write_reg(espi, ESPI_SPMODE, SPMODE_INIT_VAL | SPMODE_ENABLE);
  543. }
  544. static int fsl_espi_probe(struct device *dev, struct resource *mem,
  545. unsigned int irq, unsigned int num_cs)
  546. {
  547. struct spi_master *master;
  548. struct fsl_espi *espi;
  549. int ret;
  550. master = spi_alloc_master(dev, sizeof(struct fsl_espi));
  551. if (!master)
  552. return -ENOMEM;
  553. dev_set_drvdata(dev, master);
  554. master->mode_bits = SPI_RX_DUAL | SPI_CPOL | SPI_CPHA | SPI_CS_HIGH |
  555. SPI_LSB_FIRST | SPI_LOOP;
  556. master->dev.of_node = dev->of_node;
  557. master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 16);
  558. master->setup = fsl_espi_setup;
  559. master->cleanup = fsl_espi_cleanup;
  560. master->transfer_one_message = fsl_espi_do_one_msg;
  561. master->auto_runtime_pm = true;
  562. master->max_message_size = fsl_espi_max_message_size;
  563. master->num_chipselect = num_cs;
  564. espi = spi_master_get_devdata(master);
  565. spin_lock_init(&espi->lock);
  566. espi->dev = dev;
  567. espi->spibrg = fsl_get_sys_freq();
  568. if (espi->spibrg == -1) {
  569. dev_err(dev, "Can't get sys frequency!\n");
  570. ret = -EINVAL;
  571. goto err_probe;
  572. }
  573. /* determined by clock divider fields DIV16/PM in register SPMODEx */
  574. master->min_speed_hz = DIV_ROUND_UP(espi->spibrg, 4 * 16 * 16);
  575. master->max_speed_hz = DIV_ROUND_UP(espi->spibrg, 4);
  576. init_completion(&espi->done);
  577. espi->reg_base = devm_ioremap_resource(dev, mem);
  578. if (IS_ERR(espi->reg_base)) {
  579. ret = PTR_ERR(espi->reg_base);
  580. goto err_probe;
  581. }
  582. /* Register for SPI Interrupt */
  583. ret = devm_request_irq(dev, irq, fsl_espi_irq, 0, "fsl_espi", espi);
  584. if (ret)
  585. goto err_probe;
  586. fsl_espi_init_regs(dev, true);
  587. pm_runtime_set_autosuspend_delay(dev, AUTOSUSPEND_TIMEOUT);
  588. pm_runtime_use_autosuspend(dev);
  589. pm_runtime_set_active(dev);
  590. pm_runtime_enable(dev);
  591. pm_runtime_get_sync(dev);
  592. ret = devm_spi_register_master(dev, master);
  593. if (ret < 0)
  594. goto err_pm;
  595. dev_info(dev, "at 0x%p (irq = %u)\n", espi->reg_base, irq);
  596. pm_runtime_mark_last_busy(dev);
  597. pm_runtime_put_autosuspend(dev);
  598. return 0;
  599. err_pm:
  600. pm_runtime_put_noidle(dev);
  601. pm_runtime_disable(dev);
  602. pm_runtime_set_suspended(dev);
  603. err_probe:
  604. spi_master_put(master);
  605. return ret;
  606. }
  607. static int of_fsl_espi_get_chipselects(struct device *dev)
  608. {
  609. struct device_node *np = dev->of_node;
  610. u32 num_cs;
  611. int ret;
  612. ret = of_property_read_u32(np, "fsl,espi-num-chipselects", &num_cs);
  613. if (ret) {
  614. dev_err(dev, "No 'fsl,espi-num-chipselects' property\n");
  615. return 0;
  616. }
  617. return num_cs;
  618. }
  619. static int of_fsl_espi_probe(struct platform_device *ofdev)
  620. {
  621. struct device *dev = &ofdev->dev;
  622. struct device_node *np = ofdev->dev.of_node;
  623. struct resource mem;
  624. unsigned int irq, num_cs;
  625. int ret;
  626. if (of_property_read_bool(np, "mode")) {
  627. dev_err(dev, "mode property is not supported on ESPI!\n");
  628. return -EINVAL;
  629. }
  630. num_cs = of_fsl_espi_get_chipselects(dev);
  631. if (!num_cs)
  632. return -EINVAL;
  633. ret = of_address_to_resource(np, 0, &mem);
  634. if (ret)
  635. return ret;
  636. irq = irq_of_parse_and_map(np, 0);
  637. if (!irq)
  638. return -EINVAL;
  639. return fsl_espi_probe(dev, &mem, irq, num_cs);
  640. }
  641. static int of_fsl_espi_remove(struct platform_device *dev)
  642. {
  643. pm_runtime_disable(&dev->dev);
  644. return 0;
  645. }
  646. #ifdef CONFIG_PM_SLEEP
  647. static int of_fsl_espi_suspend(struct device *dev)
  648. {
  649. struct spi_master *master = dev_get_drvdata(dev);
  650. int ret;
  651. ret = spi_master_suspend(master);
  652. if (ret) {
  653. dev_warn(dev, "cannot suspend master\n");
  654. return ret;
  655. }
  656. return pm_runtime_force_suspend(dev);
  657. }
  658. static int of_fsl_espi_resume(struct device *dev)
  659. {
  660. struct spi_master *master = dev_get_drvdata(dev);
  661. int ret;
  662. fsl_espi_init_regs(dev, false);
  663. ret = pm_runtime_force_resume(dev);
  664. if (ret < 0)
  665. return ret;
  666. return spi_master_resume(master);
  667. }
  668. #endif /* CONFIG_PM_SLEEP */
  669. static const struct dev_pm_ops espi_pm = {
  670. SET_RUNTIME_PM_OPS(fsl_espi_runtime_suspend,
  671. fsl_espi_runtime_resume, NULL)
  672. SET_SYSTEM_SLEEP_PM_OPS(of_fsl_espi_suspend, of_fsl_espi_resume)
  673. };
  674. static const struct of_device_id of_fsl_espi_match[] = {
  675. { .compatible = "fsl,mpc8536-espi" },
  676. {}
  677. };
  678. MODULE_DEVICE_TABLE(of, of_fsl_espi_match);
  679. static struct platform_driver fsl_espi_driver = {
  680. .driver = {
  681. .name = "fsl_espi",
  682. .of_match_table = of_fsl_espi_match,
  683. .pm = &espi_pm,
  684. },
  685. .probe = of_fsl_espi_probe,
  686. .remove = of_fsl_espi_remove,
  687. };
  688. module_platform_driver(fsl_espi_driver);
  689. MODULE_AUTHOR("Mingkai Hu");
  690. MODULE_DESCRIPTION("Enhanced Freescale SPI Driver");
  691. MODULE_LICENSE("GPL");