spi-fsl-espi.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791
  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. #include "spi-fsl-lib.h"
  26. /* eSPI Controller registers */
  27. #define ESPI_SPMODE 0x00 /* eSPI mode register */
  28. #define ESPI_SPIE 0x04 /* eSPI event register */
  29. #define ESPI_SPIM 0x08 /* eSPI mask register */
  30. #define ESPI_SPCOM 0x0c /* eSPI command register */
  31. #define ESPI_SPITF 0x10 /* eSPI transmit FIFO access register*/
  32. #define ESPI_SPIRF 0x14 /* eSPI receive FIFO access register*/
  33. #define ESPI_SPMODE0 0x20 /* eSPI cs0 mode register */
  34. #define ESPI_SPMODEx(x) (ESPI_SPMODE0 + (x) * 4)
  35. /* eSPI Controller mode register definitions */
  36. #define SPMODE_ENABLE BIT(31)
  37. #define SPMODE_LOOP BIT(30)
  38. #define SPMODE_TXTHR(x) ((x) << 8)
  39. #define SPMODE_RXTHR(x) ((x) << 0)
  40. /* eSPI Controller CS mode register definitions */
  41. #define CSMODE_CI_INACTIVEHIGH BIT(31)
  42. #define CSMODE_CP_BEGIN_EDGECLK BIT(30)
  43. #define CSMODE_REV BIT(29)
  44. #define CSMODE_DIV16 BIT(28)
  45. #define CSMODE_PM(x) ((x) << 24)
  46. #define CSMODE_POL_1 BIT(20)
  47. #define CSMODE_LEN(x) ((x) << 16)
  48. #define CSMODE_BEF(x) ((x) << 12)
  49. #define CSMODE_AFT(x) ((x) << 8)
  50. #define CSMODE_CG(x) ((x) << 3)
  51. /* Default mode/csmode for eSPI controller */
  52. #define SPMODE_INIT_VAL (SPMODE_TXTHR(4) | SPMODE_RXTHR(3))
  53. #define CSMODE_INIT_VAL (CSMODE_POL_1 | CSMODE_BEF(0) \
  54. | CSMODE_AFT(0) | CSMODE_CG(1))
  55. /* SPIE register values */
  56. #define SPIE_RXCNT(reg) ((reg >> 24) & 0x3F)
  57. #define SPIE_TXCNT(reg) ((reg >> 16) & 0x3F)
  58. #define SPIE_TXE BIT(15) /* TX FIFO empty */
  59. #define SPIE_DON BIT(14) /* TX done */
  60. #define SPIE_RXT BIT(13) /* RX FIFO threshold */
  61. #define SPIE_RXF BIT(12) /* RX FIFO full */
  62. #define SPIE_TXT BIT(11) /* TX FIFO threshold*/
  63. #define SPIE_RNE BIT(9) /* RX FIFO not empty */
  64. #define SPIE_TNF BIT(8) /* TX FIFO not full */
  65. /* SPIM register values */
  66. #define SPIM_TXE BIT(15) /* TX FIFO empty */
  67. #define SPIM_DON BIT(14) /* TX done */
  68. #define SPIM_RXT BIT(13) /* RX FIFO threshold */
  69. #define SPIM_RXF BIT(12) /* RX FIFO full */
  70. #define SPIM_TXT BIT(11) /* TX FIFO threshold*/
  71. #define SPIM_RNE BIT(9) /* RX FIFO not empty */
  72. #define SPIM_TNF BIT(8) /* TX FIFO not full */
  73. /* SPCOM register values */
  74. #define SPCOM_CS(x) ((x) << 30)
  75. #define SPCOM_DO BIT(28) /* Dual output */
  76. #define SPCOM_TO BIT(27) /* TX only */
  77. #define SPCOM_RXSKIP(x) ((x) << 16)
  78. #define SPCOM_TRANLEN(x) ((x) << 0)
  79. #define SPCOM_TRANLEN_MAX 0x10000 /* Max transaction length */
  80. #define AUTOSUSPEND_TIMEOUT 2000
  81. static inline u32 fsl_espi_read_reg(struct mpc8xxx_spi *mspi, int offset)
  82. {
  83. return ioread32be(mspi->reg_base + offset);
  84. }
  85. static inline u8 fsl_espi_read_reg8(struct mpc8xxx_spi *mspi, int offset)
  86. {
  87. return ioread8(mspi->reg_base + offset);
  88. }
  89. static inline void fsl_espi_write_reg(struct mpc8xxx_spi *mspi, int offset,
  90. u32 val)
  91. {
  92. iowrite32be(val, mspi->reg_base + offset);
  93. }
  94. static inline void fsl_espi_write_reg8(struct mpc8xxx_spi *mspi, int offset,
  95. u8 val)
  96. {
  97. iowrite8(val, mspi->reg_base + offset);
  98. }
  99. static void fsl_espi_copy_to_buf(struct spi_message *m,
  100. struct mpc8xxx_spi *mspi)
  101. {
  102. struct spi_transfer *t;
  103. u8 *buf = mspi->local_buf;
  104. list_for_each_entry(t, &m->transfers, transfer_list) {
  105. if (t->tx_buf)
  106. memcpy(buf, t->tx_buf, t->len);
  107. else
  108. memset(buf, 0, t->len);
  109. buf += t->len;
  110. }
  111. }
  112. static void fsl_espi_copy_from_buf(struct spi_message *m,
  113. struct mpc8xxx_spi *mspi)
  114. {
  115. struct spi_transfer *t;
  116. u8 *buf = mspi->local_buf;
  117. list_for_each_entry(t, &m->transfers, transfer_list) {
  118. if (t->rx_buf)
  119. memcpy(t->rx_buf, buf, t->len);
  120. buf += t->len;
  121. }
  122. }
  123. static int fsl_espi_check_message(struct spi_message *m)
  124. {
  125. struct mpc8xxx_spi *mspi = spi_master_get_devdata(m->spi->master);
  126. struct spi_transfer *t, *first;
  127. if (m->frame_length > SPCOM_TRANLEN_MAX) {
  128. dev_err(mspi->dev, "message too long, size is %u bytes\n",
  129. m->frame_length);
  130. return -EMSGSIZE;
  131. }
  132. first = list_first_entry(&m->transfers, struct spi_transfer,
  133. transfer_list);
  134. list_for_each_entry(t, &m->transfers, transfer_list) {
  135. if (first->bits_per_word != t->bits_per_word ||
  136. first->speed_hz != t->speed_hz) {
  137. dev_err(mspi->dev, "bits_per_word/speed_hz should be the same for all transfers\n");
  138. return -EINVAL;
  139. }
  140. }
  141. return 0;
  142. }
  143. static void fsl_espi_change_mode(struct spi_device *spi)
  144. {
  145. struct mpc8xxx_spi *mspi = spi_master_get_devdata(spi->master);
  146. struct spi_mpc8xxx_cs *cs = spi->controller_state;
  147. u32 tmp;
  148. unsigned long flags;
  149. /* Turn off IRQs locally to minimize time that SPI is disabled. */
  150. local_irq_save(flags);
  151. /* Turn off SPI unit prior changing mode */
  152. tmp = fsl_espi_read_reg(mspi, ESPI_SPMODE);
  153. fsl_espi_write_reg(mspi, ESPI_SPMODE, tmp & ~SPMODE_ENABLE);
  154. fsl_espi_write_reg(mspi, ESPI_SPMODEx(spi->chip_select),
  155. cs->hw_mode);
  156. fsl_espi_write_reg(mspi, ESPI_SPMODE, tmp);
  157. local_irq_restore(flags);
  158. }
  159. static u32 fsl_espi_tx_buf_lsb(struct mpc8xxx_spi *mpc8xxx_spi)
  160. {
  161. u32 data;
  162. u16 data_h;
  163. u16 data_l;
  164. const u32 *tx = mpc8xxx_spi->tx;
  165. if (!tx)
  166. return 0;
  167. data = *tx++ << mpc8xxx_spi->tx_shift;
  168. data_l = data & 0xffff;
  169. data_h = (data >> 16) & 0xffff;
  170. swab16s(&data_l);
  171. swab16s(&data_h);
  172. data = data_h | data_l;
  173. mpc8xxx_spi->tx = tx;
  174. return data;
  175. }
  176. static void fsl_espi_setup_transfer(struct spi_device *spi,
  177. struct spi_transfer *t)
  178. {
  179. struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(spi->master);
  180. int bits_per_word = t ? t->bits_per_word : spi->bits_per_word;
  181. u32 hz = t ? t->speed_hz : spi->max_speed_hz;
  182. u8 pm;
  183. struct spi_mpc8xxx_cs *cs = spi->controller_state;
  184. cs->rx_shift = 0;
  185. cs->tx_shift = 0;
  186. cs->get_rx = mpc8xxx_spi_rx_buf_u32;
  187. cs->get_tx = mpc8xxx_spi_tx_buf_u32;
  188. if (bits_per_word <= 8) {
  189. cs->rx_shift = 8 - bits_per_word;
  190. } else {
  191. cs->rx_shift = 16 - bits_per_word;
  192. if (spi->mode & SPI_LSB_FIRST)
  193. cs->get_tx = fsl_espi_tx_buf_lsb;
  194. }
  195. mpc8xxx_spi->rx_shift = cs->rx_shift;
  196. mpc8xxx_spi->tx_shift = cs->tx_shift;
  197. mpc8xxx_spi->get_rx = cs->get_rx;
  198. mpc8xxx_spi->get_tx = cs->get_tx;
  199. /* mask out bits we are going to set */
  200. cs->hw_mode &= ~(CSMODE_LEN(0xF) | CSMODE_DIV16 | CSMODE_PM(0xF));
  201. cs->hw_mode |= CSMODE_LEN(bits_per_word - 1);
  202. if ((mpc8xxx_spi->spibrg / hz) > 64) {
  203. cs->hw_mode |= CSMODE_DIV16;
  204. pm = DIV_ROUND_UP(mpc8xxx_spi->spibrg, hz * 16 * 4);
  205. WARN_ONCE(pm > 33, "%s: Requested speed is too low: %d Hz. "
  206. "Will use %d Hz instead.\n", dev_name(&spi->dev),
  207. hz, mpc8xxx_spi->spibrg / (4 * 16 * (32 + 1)));
  208. if (pm > 33)
  209. pm = 33;
  210. } else {
  211. pm = DIV_ROUND_UP(mpc8xxx_spi->spibrg, hz * 4);
  212. }
  213. if (pm)
  214. pm--;
  215. if (pm < 2)
  216. pm = 2;
  217. cs->hw_mode |= CSMODE_PM(pm);
  218. fsl_espi_change_mode(spi);
  219. }
  220. static int fsl_espi_bufs(struct spi_device *spi, struct spi_transfer *t)
  221. {
  222. struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(spi->master);
  223. u32 word;
  224. int ret;
  225. mpc8xxx_spi->len = t->len;
  226. mpc8xxx_spi->count = roundup(t->len, 4) / 4;
  227. mpc8xxx_spi->tx = t->tx_buf;
  228. mpc8xxx_spi->rx = t->rx_buf;
  229. reinit_completion(&mpc8xxx_spi->done);
  230. /* Set SPCOM[CS] and SPCOM[TRANLEN] field */
  231. fsl_espi_write_reg(mpc8xxx_spi, ESPI_SPCOM,
  232. (SPCOM_CS(spi->chip_select) | SPCOM_TRANLEN(t->len - 1)));
  233. /* enable rx ints */
  234. fsl_espi_write_reg(mpc8xxx_spi, ESPI_SPIM, SPIM_RNE);
  235. /* transmit word */
  236. word = mpc8xxx_spi->get_tx(mpc8xxx_spi);
  237. fsl_espi_write_reg(mpc8xxx_spi, ESPI_SPITF, word);
  238. /* Won't hang up forever, SPI bus sometimes got lost interrupts... */
  239. ret = wait_for_completion_timeout(&mpc8xxx_spi->done, 2 * HZ);
  240. if (ret == 0)
  241. dev_err(mpc8xxx_spi->dev,
  242. "Transaction hanging up (left %d bytes)\n",
  243. mpc8xxx_spi->count);
  244. /* disable rx ints */
  245. fsl_espi_write_reg(mpc8xxx_spi, ESPI_SPIM, 0);
  246. return mpc8xxx_spi->count > 0 ? -EMSGSIZE : 0;
  247. }
  248. static int fsl_espi_trans(struct spi_message *m, struct spi_transfer *trans)
  249. {
  250. struct mpc8xxx_spi *mspi = spi_master_get_devdata(m->spi->master);
  251. struct spi_device *spi = m->spi;
  252. int ret;
  253. fsl_espi_copy_to_buf(m, mspi);
  254. fsl_espi_setup_transfer(spi, trans);
  255. ret = fsl_espi_bufs(spi, trans);
  256. if (trans->delay_usecs)
  257. udelay(trans->delay_usecs);
  258. fsl_espi_setup_transfer(spi, NULL);
  259. if (!ret)
  260. fsl_espi_copy_from_buf(m, mspi);
  261. return ret;
  262. }
  263. static int fsl_espi_do_one_msg(struct spi_master *master,
  264. struct spi_message *m)
  265. {
  266. struct mpc8xxx_spi *mspi = spi_master_get_devdata(m->spi->master);
  267. unsigned int delay_usecs = 0;
  268. struct spi_transfer *t, trans = {};
  269. int ret;
  270. ret = fsl_espi_check_message(m);
  271. if (ret)
  272. goto out;
  273. list_for_each_entry(t, &m->transfers, transfer_list) {
  274. if (t->delay_usecs > delay_usecs)
  275. delay_usecs = t->delay_usecs;
  276. }
  277. t = list_first_entry(&m->transfers, struct spi_transfer,
  278. transfer_list);
  279. trans.len = m->frame_length;
  280. trans.speed_hz = t->speed_hz;
  281. trans.bits_per_word = t->bits_per_word;
  282. trans.delay_usecs = delay_usecs;
  283. trans.tx_buf = mspi->local_buf;
  284. trans.rx_buf = mspi->local_buf;
  285. if (trans.len)
  286. ret = fsl_espi_trans(m, &trans);
  287. m->actual_length = ret ? 0 : trans.len;
  288. out:
  289. if (m->status == -EINPROGRESS)
  290. m->status = ret;
  291. spi_finalize_current_message(master);
  292. return ret;
  293. }
  294. static int fsl_espi_setup(struct spi_device *spi)
  295. {
  296. struct mpc8xxx_spi *mpc8xxx_spi;
  297. u32 loop_mode;
  298. struct spi_mpc8xxx_cs *cs = spi_get_ctldata(spi);
  299. if (!spi->max_speed_hz)
  300. return -EINVAL;
  301. if (!cs) {
  302. cs = kzalloc(sizeof(*cs), GFP_KERNEL);
  303. if (!cs)
  304. return -ENOMEM;
  305. spi_set_ctldata(spi, cs);
  306. }
  307. mpc8xxx_spi = spi_master_get_devdata(spi->master);
  308. pm_runtime_get_sync(mpc8xxx_spi->dev);
  309. cs->hw_mode = fsl_espi_read_reg(mpc8xxx_spi,
  310. ESPI_SPMODEx(spi->chip_select));
  311. /* mask out bits we are going to set */
  312. cs->hw_mode &= ~(CSMODE_CP_BEGIN_EDGECLK | CSMODE_CI_INACTIVEHIGH
  313. | CSMODE_REV);
  314. if (spi->mode & SPI_CPHA)
  315. cs->hw_mode |= CSMODE_CP_BEGIN_EDGECLK;
  316. if (spi->mode & SPI_CPOL)
  317. cs->hw_mode |= CSMODE_CI_INACTIVEHIGH;
  318. if (!(spi->mode & SPI_LSB_FIRST))
  319. cs->hw_mode |= CSMODE_REV;
  320. /* Handle the loop mode */
  321. loop_mode = fsl_espi_read_reg(mpc8xxx_spi, ESPI_SPMODE);
  322. loop_mode &= ~SPMODE_LOOP;
  323. if (spi->mode & SPI_LOOP)
  324. loop_mode |= SPMODE_LOOP;
  325. fsl_espi_write_reg(mpc8xxx_spi, ESPI_SPMODE, loop_mode);
  326. fsl_espi_setup_transfer(spi, NULL);
  327. pm_runtime_mark_last_busy(mpc8xxx_spi->dev);
  328. pm_runtime_put_autosuspend(mpc8xxx_spi->dev);
  329. return 0;
  330. }
  331. static void fsl_espi_cleanup(struct spi_device *spi)
  332. {
  333. struct spi_mpc8xxx_cs *cs = spi_get_ctldata(spi);
  334. kfree(cs);
  335. spi_set_ctldata(spi, NULL);
  336. }
  337. static void fsl_espi_cpu_irq(struct mpc8xxx_spi *mspi, u32 events)
  338. {
  339. /* We need handle RX first */
  340. if (events & SPIE_RNE) {
  341. u32 rx_data, tmp;
  342. u8 rx_data_8;
  343. int rx_nr_bytes = 4;
  344. int ret;
  345. /* Spin until RX is done */
  346. if (SPIE_RXCNT(events) < min(4, mspi->len)) {
  347. ret = spin_event_timeout(
  348. !(SPIE_RXCNT(events =
  349. fsl_espi_read_reg(mspi, ESPI_SPIE)) <
  350. min(4, mspi->len)),
  351. 10000, 0); /* 10 msec */
  352. if (!ret)
  353. dev_err(mspi->dev,
  354. "tired waiting for SPIE_RXCNT\n");
  355. }
  356. if (mspi->len >= 4) {
  357. rx_data = fsl_espi_read_reg(mspi, ESPI_SPIRF);
  358. } else if (mspi->len <= 0) {
  359. dev_err(mspi->dev,
  360. "unexpected RX(SPIE_RNE) interrupt occurred,\n"
  361. "(local rxlen %d bytes, reg rxlen %d bytes)\n",
  362. min(4, mspi->len), SPIE_RXCNT(events));
  363. rx_nr_bytes = 0;
  364. } else {
  365. rx_nr_bytes = mspi->len;
  366. tmp = mspi->len;
  367. rx_data = 0;
  368. while (tmp--) {
  369. rx_data_8 = fsl_espi_read_reg8(mspi,
  370. ESPI_SPIRF);
  371. rx_data |= (rx_data_8 << (tmp * 8));
  372. }
  373. rx_data <<= (4 - mspi->len) * 8;
  374. }
  375. mspi->len -= rx_nr_bytes;
  376. if (rx_nr_bytes && mspi->rx)
  377. mspi->get_rx(rx_data, mspi);
  378. }
  379. if (!(events & SPIE_TNF)) {
  380. int ret;
  381. /* spin until TX is done */
  382. ret = spin_event_timeout(((events = fsl_espi_read_reg(
  383. mspi, ESPI_SPIE)) & SPIE_TNF), 1000, 0);
  384. if (!ret) {
  385. dev_err(mspi->dev, "tired waiting for SPIE_TNF\n");
  386. complete(&mspi->done);
  387. return;
  388. }
  389. }
  390. mspi->count -= 1;
  391. if (mspi->count) {
  392. u32 word = mspi->get_tx(mspi);
  393. fsl_espi_write_reg(mspi, ESPI_SPITF, word);
  394. } else {
  395. complete(&mspi->done);
  396. }
  397. }
  398. static irqreturn_t fsl_espi_irq(s32 irq, void *context_data)
  399. {
  400. struct mpc8xxx_spi *mspi = context_data;
  401. u32 events;
  402. /* Get interrupt events(tx/rx) */
  403. events = fsl_espi_read_reg(mspi, ESPI_SPIE);
  404. if (!events)
  405. return IRQ_NONE;
  406. dev_vdbg(mspi->dev, "%s: events %x\n", __func__, events);
  407. fsl_espi_cpu_irq(mspi, events);
  408. /* Clear the events */
  409. fsl_espi_write_reg(mspi, ESPI_SPIE, events);
  410. return IRQ_HANDLED;
  411. }
  412. #ifdef CONFIG_PM
  413. static int fsl_espi_runtime_suspend(struct device *dev)
  414. {
  415. struct spi_master *master = dev_get_drvdata(dev);
  416. struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(master);
  417. u32 regval;
  418. regval = fsl_espi_read_reg(mpc8xxx_spi, ESPI_SPMODE);
  419. regval &= ~SPMODE_ENABLE;
  420. fsl_espi_write_reg(mpc8xxx_spi, ESPI_SPMODE, regval);
  421. return 0;
  422. }
  423. static int fsl_espi_runtime_resume(struct device *dev)
  424. {
  425. struct spi_master *master = dev_get_drvdata(dev);
  426. struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(master);
  427. u32 regval;
  428. regval = fsl_espi_read_reg(mpc8xxx_spi, ESPI_SPMODE);
  429. regval |= SPMODE_ENABLE;
  430. fsl_espi_write_reg(mpc8xxx_spi, ESPI_SPMODE, regval);
  431. return 0;
  432. }
  433. #endif
  434. static size_t fsl_espi_max_message_size(struct spi_device *spi)
  435. {
  436. return SPCOM_TRANLEN_MAX;
  437. }
  438. static int fsl_espi_probe(struct device *dev, struct resource *mem,
  439. unsigned int irq)
  440. {
  441. struct fsl_spi_platform_data *pdata = dev_get_platdata(dev);
  442. struct spi_master *master;
  443. struct mpc8xxx_spi *mpc8xxx_spi;
  444. struct device_node *nc;
  445. const __be32 *prop;
  446. u32 regval, csmode;
  447. int i, len, ret;
  448. master = spi_alloc_master(dev, sizeof(struct mpc8xxx_spi));
  449. if (!master)
  450. return -ENOMEM;
  451. dev_set_drvdata(dev, master);
  452. mpc8xxx_spi_probe(dev, mem, irq);
  453. master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 16);
  454. master->setup = fsl_espi_setup;
  455. master->cleanup = fsl_espi_cleanup;
  456. master->transfer_one_message = fsl_espi_do_one_msg;
  457. master->auto_runtime_pm = true;
  458. master->max_message_size = fsl_espi_max_message_size;
  459. mpc8xxx_spi = spi_master_get_devdata(master);
  460. mpc8xxx_spi->local_buf =
  461. devm_kmalloc(dev, SPCOM_TRANLEN_MAX, GFP_KERNEL);
  462. if (!mpc8xxx_spi->local_buf) {
  463. ret = -ENOMEM;
  464. goto err_probe;
  465. }
  466. mpc8xxx_spi->reg_base = devm_ioremap_resource(dev, mem);
  467. if (IS_ERR(mpc8xxx_spi->reg_base)) {
  468. ret = PTR_ERR(mpc8xxx_spi->reg_base);
  469. goto err_probe;
  470. }
  471. /* Register for SPI Interrupt */
  472. ret = devm_request_irq(dev, mpc8xxx_spi->irq, fsl_espi_irq,
  473. 0, "fsl_espi", mpc8xxx_spi);
  474. if (ret)
  475. goto err_probe;
  476. if (mpc8xxx_spi->flags & SPI_QE_CPU_MODE) {
  477. mpc8xxx_spi->rx_shift = 16;
  478. mpc8xxx_spi->tx_shift = 24;
  479. }
  480. /* SPI controller initializations */
  481. fsl_espi_write_reg(mpc8xxx_spi, ESPI_SPMODE, 0);
  482. fsl_espi_write_reg(mpc8xxx_spi, ESPI_SPIM, 0);
  483. fsl_espi_write_reg(mpc8xxx_spi, ESPI_SPCOM, 0);
  484. fsl_espi_write_reg(mpc8xxx_spi, ESPI_SPIE, 0xffffffff);
  485. /* Init eSPI CS mode register */
  486. for_each_available_child_of_node(master->dev.of_node, nc) {
  487. /* get chip select */
  488. prop = of_get_property(nc, "reg", &len);
  489. if (!prop || len < sizeof(*prop))
  490. continue;
  491. i = be32_to_cpup(prop);
  492. if (i < 0 || i >= pdata->max_chipselect)
  493. continue;
  494. csmode = CSMODE_INIT_VAL;
  495. /* check if CSBEF is set in device tree */
  496. prop = of_get_property(nc, "fsl,csbef", &len);
  497. if (prop && len >= sizeof(*prop)) {
  498. csmode &= ~(CSMODE_BEF(0xf));
  499. csmode |= CSMODE_BEF(be32_to_cpup(prop));
  500. }
  501. /* check if CSAFT is set in device tree */
  502. prop = of_get_property(nc, "fsl,csaft", &len);
  503. if (prop && len >= sizeof(*prop)) {
  504. csmode &= ~(CSMODE_AFT(0xf));
  505. csmode |= CSMODE_AFT(be32_to_cpup(prop));
  506. }
  507. fsl_espi_write_reg(mpc8xxx_spi, ESPI_SPMODEx(i), csmode);
  508. dev_info(dev, "cs=%d, init_csmode=0x%x\n", i, csmode);
  509. }
  510. /* Enable SPI interface */
  511. regval = pdata->initial_spmode | SPMODE_INIT_VAL | SPMODE_ENABLE;
  512. fsl_espi_write_reg(mpc8xxx_spi, ESPI_SPMODE, regval);
  513. pm_runtime_set_autosuspend_delay(dev, AUTOSUSPEND_TIMEOUT);
  514. pm_runtime_use_autosuspend(dev);
  515. pm_runtime_set_active(dev);
  516. pm_runtime_enable(dev);
  517. pm_runtime_get_sync(dev);
  518. ret = devm_spi_register_master(dev, master);
  519. if (ret < 0)
  520. goto err_pm;
  521. dev_info(dev, "at 0x%p (irq = %d)\n", mpc8xxx_spi->reg_base,
  522. mpc8xxx_spi->irq);
  523. pm_runtime_mark_last_busy(dev);
  524. pm_runtime_put_autosuspend(dev);
  525. return 0;
  526. err_pm:
  527. pm_runtime_put_noidle(dev);
  528. pm_runtime_disable(dev);
  529. pm_runtime_set_suspended(dev);
  530. err_probe:
  531. spi_master_put(master);
  532. return ret;
  533. }
  534. static int of_fsl_espi_get_chipselects(struct device *dev)
  535. {
  536. struct device_node *np = dev->of_node;
  537. struct fsl_spi_platform_data *pdata = dev_get_platdata(dev);
  538. const u32 *prop;
  539. int len;
  540. prop = of_get_property(np, "fsl,espi-num-chipselects", &len);
  541. if (!prop || len < sizeof(*prop)) {
  542. dev_err(dev, "No 'fsl,espi-num-chipselects' property\n");
  543. return -EINVAL;
  544. }
  545. pdata->max_chipselect = *prop;
  546. pdata->cs_control = NULL;
  547. return 0;
  548. }
  549. static int of_fsl_espi_probe(struct platform_device *ofdev)
  550. {
  551. struct device *dev = &ofdev->dev;
  552. struct device_node *np = ofdev->dev.of_node;
  553. struct resource mem;
  554. unsigned int irq;
  555. int ret;
  556. ret = of_mpc8xxx_spi_probe(ofdev);
  557. if (ret)
  558. return ret;
  559. ret = of_fsl_espi_get_chipselects(dev);
  560. if (ret)
  561. return ret;
  562. ret = of_address_to_resource(np, 0, &mem);
  563. if (ret)
  564. return ret;
  565. irq = irq_of_parse_and_map(np, 0);
  566. if (!irq)
  567. return -EINVAL;
  568. return fsl_espi_probe(dev, &mem, irq);
  569. }
  570. static int of_fsl_espi_remove(struct platform_device *dev)
  571. {
  572. pm_runtime_disable(&dev->dev);
  573. return 0;
  574. }
  575. #ifdef CONFIG_PM_SLEEP
  576. static int of_fsl_espi_suspend(struct device *dev)
  577. {
  578. struct spi_master *master = dev_get_drvdata(dev);
  579. int ret;
  580. ret = spi_master_suspend(master);
  581. if (ret) {
  582. dev_warn(dev, "cannot suspend master\n");
  583. return ret;
  584. }
  585. ret = pm_runtime_force_suspend(dev);
  586. if (ret < 0)
  587. return ret;
  588. return 0;
  589. }
  590. static int of_fsl_espi_resume(struct device *dev)
  591. {
  592. struct fsl_spi_platform_data *pdata = dev_get_platdata(dev);
  593. struct spi_master *master = dev_get_drvdata(dev);
  594. struct mpc8xxx_spi *mpc8xxx_spi;
  595. u32 regval;
  596. int i, ret;
  597. mpc8xxx_spi = spi_master_get_devdata(master);
  598. /* SPI controller initializations */
  599. fsl_espi_write_reg(mpc8xxx_spi, ESPI_SPMODE, 0);
  600. fsl_espi_write_reg(mpc8xxx_spi, ESPI_SPIM, 0);
  601. fsl_espi_write_reg(mpc8xxx_spi, ESPI_SPCOM, 0);
  602. fsl_espi_write_reg(mpc8xxx_spi, ESPI_SPIE, 0xffffffff);
  603. /* Init eSPI CS mode register */
  604. for (i = 0; i < pdata->max_chipselect; i++)
  605. fsl_espi_write_reg(mpc8xxx_spi, ESPI_SPMODEx(i),
  606. CSMODE_INIT_VAL);
  607. /* Enable SPI interface */
  608. regval = pdata->initial_spmode | SPMODE_INIT_VAL | SPMODE_ENABLE;
  609. fsl_espi_write_reg(mpc8xxx_spi, ESPI_SPMODE, regval);
  610. ret = pm_runtime_force_resume(dev);
  611. if (ret < 0)
  612. return ret;
  613. return spi_master_resume(master);
  614. }
  615. #endif /* CONFIG_PM_SLEEP */
  616. static const struct dev_pm_ops espi_pm = {
  617. SET_RUNTIME_PM_OPS(fsl_espi_runtime_suspend,
  618. fsl_espi_runtime_resume, NULL)
  619. SET_SYSTEM_SLEEP_PM_OPS(of_fsl_espi_suspend, of_fsl_espi_resume)
  620. };
  621. static const struct of_device_id of_fsl_espi_match[] = {
  622. { .compatible = "fsl,mpc8536-espi" },
  623. {}
  624. };
  625. MODULE_DEVICE_TABLE(of, of_fsl_espi_match);
  626. static struct platform_driver fsl_espi_driver = {
  627. .driver = {
  628. .name = "fsl_espi",
  629. .of_match_table = of_fsl_espi_match,
  630. .pm = &espi_pm,
  631. },
  632. .probe = of_fsl_espi_probe,
  633. .remove = of_fsl_espi_remove,
  634. };
  635. module_platform_driver(fsl_espi_driver);
  636. MODULE_AUTHOR("Mingkai Hu");
  637. MODULE_DESCRIPTION("Enhanced Freescale SPI Driver");
  638. MODULE_LICENSE("GPL");