spi-fsl-espi.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905
  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/irq.h>
  16. #include <linux/module.h>
  17. #include <linux/mm.h>
  18. #include <linux/of.h>
  19. #include <linux/of_address.h>
  20. #include <linux/of_irq.h>
  21. #include <linux/of_platform.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/spi/spi.h>
  24. #include <linux/pm_runtime.h>
  25. #include <sysdev/fsl_soc.h>
  26. #include "spi-fsl-lib.h"
  27. /* eSPI Controller registers */
  28. struct fsl_espi_reg {
  29. __be32 mode; /* 0x000 - eSPI mode register */
  30. __be32 event; /* 0x004 - eSPI event register */
  31. __be32 mask; /* 0x008 - eSPI mask register */
  32. __be32 command; /* 0x00c - eSPI command register */
  33. __be32 transmit; /* 0x010 - eSPI transmit FIFO access register*/
  34. __be32 receive; /* 0x014 - eSPI receive FIFO access register*/
  35. u8 res[8]; /* 0x018 - 0x01c reserved */
  36. __be32 csmode[4]; /* 0x020 - 0x02c eSPI cs mode register */
  37. };
  38. struct fsl_espi_transfer {
  39. const void *tx_buf;
  40. void *rx_buf;
  41. unsigned len;
  42. unsigned n_tx;
  43. unsigned n_rx;
  44. unsigned actual_length;
  45. int status;
  46. };
  47. /* eSPI Controller mode register definitions */
  48. #define SPMODE_ENABLE (1 << 31)
  49. #define SPMODE_LOOP (1 << 30)
  50. #define SPMODE_TXTHR(x) ((x) << 8)
  51. #define SPMODE_RXTHR(x) ((x) << 0)
  52. /* eSPI Controller CS mode register definitions */
  53. #define CSMODE_CI_INACTIVEHIGH (1 << 31)
  54. #define CSMODE_CP_BEGIN_EDGECLK (1 << 30)
  55. #define CSMODE_REV (1 << 29)
  56. #define CSMODE_DIV16 (1 << 28)
  57. #define CSMODE_PM(x) ((x) << 24)
  58. #define CSMODE_POL_1 (1 << 20)
  59. #define CSMODE_LEN(x) ((x) << 16)
  60. #define CSMODE_BEF(x) ((x) << 12)
  61. #define CSMODE_AFT(x) ((x) << 8)
  62. #define CSMODE_CG(x) ((x) << 3)
  63. /* Default mode/csmode for eSPI controller */
  64. #define SPMODE_INIT_VAL (SPMODE_TXTHR(4) | SPMODE_RXTHR(3))
  65. #define CSMODE_INIT_VAL (CSMODE_POL_1 | CSMODE_BEF(0) \
  66. | CSMODE_AFT(0) | CSMODE_CG(1))
  67. /* SPIE register values */
  68. #define SPIE_NE 0x00000200 /* Not empty */
  69. #define SPIE_NF 0x00000100 /* Not full */
  70. /* SPIM register values */
  71. #define SPIM_NE 0x00000200 /* Not empty */
  72. #define SPIM_NF 0x00000100 /* Not full */
  73. #define SPIE_RXCNT(reg) ((reg >> 24) & 0x3F)
  74. #define SPIE_TXCNT(reg) ((reg >> 16) & 0x3F)
  75. /* SPCOM register values */
  76. #define SPCOM_CS(x) ((x) << 30)
  77. #define SPCOM_TRANLEN(x) ((x) << 0)
  78. #define SPCOM_TRANLEN_MAX 0xFFFF /* Max transaction length */
  79. #define AUTOSUSPEND_TIMEOUT 2000
  80. static void fsl_espi_change_mode(struct spi_device *spi)
  81. {
  82. struct mpc8xxx_spi *mspi = spi_master_get_devdata(spi->master);
  83. struct spi_mpc8xxx_cs *cs = spi->controller_state;
  84. struct fsl_espi_reg *reg_base = mspi->reg_base;
  85. __be32 __iomem *mode = &reg_base->csmode[spi->chip_select];
  86. __be32 __iomem *espi_mode = &reg_base->mode;
  87. u32 tmp;
  88. unsigned long flags;
  89. /* Turn off IRQs locally to minimize time that SPI is disabled. */
  90. local_irq_save(flags);
  91. /* Turn off SPI unit prior changing mode */
  92. tmp = mpc8xxx_spi_read_reg(espi_mode);
  93. mpc8xxx_spi_write_reg(espi_mode, tmp & ~SPMODE_ENABLE);
  94. mpc8xxx_spi_write_reg(mode, cs->hw_mode);
  95. mpc8xxx_spi_write_reg(espi_mode, tmp);
  96. local_irq_restore(flags);
  97. }
  98. static u32 fsl_espi_tx_buf_lsb(struct mpc8xxx_spi *mpc8xxx_spi)
  99. {
  100. u32 data;
  101. u16 data_h;
  102. u16 data_l;
  103. const u32 *tx = mpc8xxx_spi->tx;
  104. if (!tx)
  105. return 0;
  106. data = *tx++ << mpc8xxx_spi->tx_shift;
  107. data_l = data & 0xffff;
  108. data_h = (data >> 16) & 0xffff;
  109. swab16s(&data_l);
  110. swab16s(&data_h);
  111. data = data_h | data_l;
  112. mpc8xxx_spi->tx = tx;
  113. return data;
  114. }
  115. static int fsl_espi_setup_transfer(struct spi_device *spi,
  116. struct spi_transfer *t)
  117. {
  118. struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(spi->master);
  119. int bits_per_word = 0;
  120. u8 pm;
  121. u32 hz = 0;
  122. struct spi_mpc8xxx_cs *cs = spi->controller_state;
  123. if (t) {
  124. bits_per_word = t->bits_per_word;
  125. hz = t->speed_hz;
  126. }
  127. /* spi_transfer level calls that work per-word */
  128. if (!bits_per_word)
  129. bits_per_word = spi->bits_per_word;
  130. if (!hz)
  131. hz = spi->max_speed_hz;
  132. cs->rx_shift = 0;
  133. cs->tx_shift = 0;
  134. cs->get_rx = mpc8xxx_spi_rx_buf_u32;
  135. cs->get_tx = mpc8xxx_spi_tx_buf_u32;
  136. if (bits_per_word <= 8) {
  137. cs->rx_shift = 8 - bits_per_word;
  138. } else {
  139. cs->rx_shift = 16 - bits_per_word;
  140. if (spi->mode & SPI_LSB_FIRST)
  141. cs->get_tx = fsl_espi_tx_buf_lsb;
  142. }
  143. mpc8xxx_spi->rx_shift = cs->rx_shift;
  144. mpc8xxx_spi->tx_shift = cs->tx_shift;
  145. mpc8xxx_spi->get_rx = cs->get_rx;
  146. mpc8xxx_spi->get_tx = cs->get_tx;
  147. bits_per_word = bits_per_word - 1;
  148. /* mask out bits we are going to set */
  149. cs->hw_mode &= ~(CSMODE_LEN(0xF) | CSMODE_DIV16 | CSMODE_PM(0xF));
  150. cs->hw_mode |= CSMODE_LEN(bits_per_word);
  151. if ((mpc8xxx_spi->spibrg / hz) > 64) {
  152. cs->hw_mode |= CSMODE_DIV16;
  153. pm = DIV_ROUND_UP(mpc8xxx_spi->spibrg, hz * 16 * 4);
  154. WARN_ONCE(pm > 33, "%s: Requested speed is too low: %d Hz. "
  155. "Will use %d Hz instead.\n", dev_name(&spi->dev),
  156. hz, mpc8xxx_spi->spibrg / (4 * 16 * (32 + 1)));
  157. if (pm > 33)
  158. pm = 33;
  159. } else {
  160. pm = DIV_ROUND_UP(mpc8xxx_spi->spibrg, hz * 4);
  161. }
  162. if (pm)
  163. pm--;
  164. if (pm < 2)
  165. pm = 2;
  166. cs->hw_mode |= CSMODE_PM(pm);
  167. fsl_espi_change_mode(spi);
  168. return 0;
  169. }
  170. static int fsl_espi_cpu_bufs(struct mpc8xxx_spi *mspi, struct spi_transfer *t,
  171. unsigned int len)
  172. {
  173. u32 word;
  174. struct fsl_espi_reg *reg_base = mspi->reg_base;
  175. mspi->count = len;
  176. /* enable rx ints */
  177. mpc8xxx_spi_write_reg(&reg_base->mask, SPIM_NE);
  178. /* transmit word */
  179. word = mspi->get_tx(mspi);
  180. mpc8xxx_spi_write_reg(&reg_base->transmit, word);
  181. return 0;
  182. }
  183. static int fsl_espi_bufs(struct spi_device *spi, struct spi_transfer *t)
  184. {
  185. struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(spi->master);
  186. struct fsl_espi_reg *reg_base = mpc8xxx_spi->reg_base;
  187. unsigned int len = t->len;
  188. int ret;
  189. mpc8xxx_spi->len = t->len;
  190. len = roundup(len, 4) / 4;
  191. mpc8xxx_spi->tx = t->tx_buf;
  192. mpc8xxx_spi->rx = t->rx_buf;
  193. reinit_completion(&mpc8xxx_spi->done);
  194. /* Set SPCOM[CS] and SPCOM[TRANLEN] field */
  195. if ((t->len - 1) > SPCOM_TRANLEN_MAX) {
  196. dev_err(mpc8xxx_spi->dev, "Transaction length (%d)"
  197. " beyond the SPCOM[TRANLEN] field\n", t->len);
  198. return -EINVAL;
  199. }
  200. mpc8xxx_spi_write_reg(&reg_base->command,
  201. (SPCOM_CS(spi->chip_select) | SPCOM_TRANLEN(t->len - 1)));
  202. ret = fsl_espi_cpu_bufs(mpc8xxx_spi, t, len);
  203. if (ret)
  204. return ret;
  205. wait_for_completion(&mpc8xxx_spi->done);
  206. /* disable rx ints */
  207. mpc8xxx_spi_write_reg(&reg_base->mask, 0);
  208. return mpc8xxx_spi->count;
  209. }
  210. static inline void fsl_espi_addr2cmd(unsigned int addr, u8 *cmd)
  211. {
  212. if (cmd) {
  213. cmd[1] = (u8)(addr >> 16);
  214. cmd[2] = (u8)(addr >> 8);
  215. cmd[3] = (u8)(addr >> 0);
  216. }
  217. }
  218. static inline unsigned int fsl_espi_cmd2addr(u8 *cmd)
  219. {
  220. if (cmd)
  221. return cmd[1] << 16 | cmd[2] << 8 | cmd[3] << 0;
  222. return 0;
  223. }
  224. static void fsl_espi_do_trans(struct spi_message *m,
  225. struct fsl_espi_transfer *tr)
  226. {
  227. struct spi_device *spi = m->spi;
  228. struct mpc8xxx_spi *mspi = spi_master_get_devdata(spi->master);
  229. struct fsl_espi_transfer *espi_trans = tr;
  230. struct spi_message message;
  231. struct spi_transfer *t, *first, trans;
  232. int status = 0;
  233. spi_message_init(&message);
  234. memset(&trans, 0, sizeof(trans));
  235. first = list_first_entry(&m->transfers, struct spi_transfer,
  236. transfer_list);
  237. list_for_each_entry(t, &m->transfers, transfer_list) {
  238. if ((first->bits_per_word != t->bits_per_word) ||
  239. (first->speed_hz != t->speed_hz)) {
  240. espi_trans->status = -EINVAL;
  241. dev_err(mspi->dev,
  242. "bits_per_word/speed_hz should be same for the same SPI transfer\n");
  243. return;
  244. }
  245. trans.speed_hz = t->speed_hz;
  246. trans.bits_per_word = t->bits_per_word;
  247. trans.delay_usecs = max(first->delay_usecs, t->delay_usecs);
  248. }
  249. trans.len = espi_trans->len;
  250. trans.tx_buf = espi_trans->tx_buf;
  251. trans.rx_buf = espi_trans->rx_buf;
  252. spi_message_add_tail(&trans, &message);
  253. list_for_each_entry(t, &message.transfers, transfer_list) {
  254. if (t->bits_per_word || t->speed_hz) {
  255. status = -EINVAL;
  256. status = fsl_espi_setup_transfer(spi, t);
  257. if (status < 0)
  258. break;
  259. }
  260. if (t->len)
  261. status = fsl_espi_bufs(spi, t);
  262. if (status) {
  263. status = -EMSGSIZE;
  264. break;
  265. }
  266. if (t->delay_usecs)
  267. udelay(t->delay_usecs);
  268. }
  269. espi_trans->status = status;
  270. fsl_espi_setup_transfer(spi, NULL);
  271. }
  272. static void fsl_espi_cmd_trans(struct spi_message *m,
  273. struct fsl_espi_transfer *trans, u8 *rx_buff)
  274. {
  275. struct spi_transfer *t;
  276. u8 *local_buf;
  277. int i = 0;
  278. struct fsl_espi_transfer *espi_trans = trans;
  279. local_buf = kzalloc(SPCOM_TRANLEN_MAX, GFP_KERNEL);
  280. if (!local_buf) {
  281. espi_trans->status = -ENOMEM;
  282. return;
  283. }
  284. list_for_each_entry(t, &m->transfers, transfer_list) {
  285. if (t->tx_buf) {
  286. memcpy(local_buf + i, t->tx_buf, t->len);
  287. i += t->len;
  288. }
  289. }
  290. espi_trans->tx_buf = local_buf;
  291. espi_trans->rx_buf = local_buf;
  292. fsl_espi_do_trans(m, espi_trans);
  293. espi_trans->actual_length = espi_trans->len;
  294. kfree(local_buf);
  295. }
  296. static void fsl_espi_rw_trans(struct spi_message *m,
  297. struct fsl_espi_transfer *trans, u8 *rx_buff)
  298. {
  299. struct fsl_espi_transfer *espi_trans = trans;
  300. unsigned int total_len = espi_trans->len;
  301. struct spi_transfer *t;
  302. u8 *local_buf;
  303. u8 *rx_buf = rx_buff;
  304. unsigned int trans_len;
  305. unsigned int addr;
  306. unsigned int tx_only;
  307. unsigned int rx_pos = 0;
  308. unsigned int pos;
  309. int i, loop;
  310. local_buf = kzalloc(SPCOM_TRANLEN_MAX, GFP_KERNEL);
  311. if (!local_buf) {
  312. espi_trans->status = -ENOMEM;
  313. return;
  314. }
  315. for (pos = 0, loop = 0; pos < total_len; pos += trans_len, loop++) {
  316. trans_len = total_len - pos;
  317. i = 0;
  318. tx_only = 0;
  319. list_for_each_entry(t, &m->transfers, transfer_list) {
  320. if (t->tx_buf) {
  321. memcpy(local_buf + i, t->tx_buf, t->len);
  322. i += t->len;
  323. if (!t->rx_buf)
  324. tx_only += t->len;
  325. }
  326. }
  327. /* Add additional TX bytes to compensate SPCOM_TRANLEN_MAX */
  328. if (loop > 0)
  329. trans_len += tx_only;
  330. if (trans_len > SPCOM_TRANLEN_MAX)
  331. trans_len = SPCOM_TRANLEN_MAX;
  332. /* Update device offset */
  333. if (pos > 0) {
  334. addr = fsl_espi_cmd2addr(local_buf);
  335. addr += rx_pos;
  336. fsl_espi_addr2cmd(addr, local_buf);
  337. }
  338. espi_trans->len = trans_len;
  339. espi_trans->tx_buf = local_buf;
  340. espi_trans->rx_buf = local_buf;
  341. fsl_espi_do_trans(m, espi_trans);
  342. /* If there is at least one RX byte then copy it to rx_buf */
  343. if (tx_only < SPCOM_TRANLEN_MAX)
  344. memcpy(rx_buf + rx_pos, espi_trans->rx_buf + tx_only,
  345. trans_len - tx_only);
  346. rx_pos += trans_len - tx_only;
  347. if (loop > 0)
  348. espi_trans->actual_length += espi_trans->len - tx_only;
  349. else
  350. espi_trans->actual_length += espi_trans->len;
  351. }
  352. kfree(local_buf);
  353. }
  354. static int fsl_espi_do_one_msg(struct spi_master *master,
  355. struct spi_message *m)
  356. {
  357. struct spi_transfer *t;
  358. u8 *rx_buf = NULL;
  359. unsigned int n_tx = 0;
  360. unsigned int n_rx = 0;
  361. unsigned int xfer_len = 0;
  362. struct fsl_espi_transfer espi_trans;
  363. list_for_each_entry(t, &m->transfers, transfer_list) {
  364. if (t->tx_buf)
  365. n_tx += t->len;
  366. if (t->rx_buf) {
  367. n_rx += t->len;
  368. rx_buf = t->rx_buf;
  369. }
  370. if ((t->tx_buf) || (t->rx_buf))
  371. xfer_len += t->len;
  372. }
  373. espi_trans.n_tx = n_tx;
  374. espi_trans.n_rx = n_rx;
  375. espi_trans.len = xfer_len;
  376. espi_trans.actual_length = 0;
  377. espi_trans.status = 0;
  378. if (!rx_buf)
  379. fsl_espi_cmd_trans(m, &espi_trans, NULL);
  380. else
  381. fsl_espi_rw_trans(m, &espi_trans, rx_buf);
  382. m->actual_length = espi_trans.actual_length;
  383. m->status = espi_trans.status;
  384. spi_finalize_current_message(master);
  385. return 0;
  386. }
  387. static int fsl_espi_setup(struct spi_device *spi)
  388. {
  389. struct mpc8xxx_spi *mpc8xxx_spi;
  390. struct fsl_espi_reg *reg_base;
  391. int retval;
  392. u32 hw_mode;
  393. u32 loop_mode;
  394. struct spi_mpc8xxx_cs *cs = spi_get_ctldata(spi);
  395. if (!spi->max_speed_hz)
  396. return -EINVAL;
  397. if (!cs) {
  398. cs = kzalloc(sizeof(*cs), GFP_KERNEL);
  399. if (!cs)
  400. return -ENOMEM;
  401. spi_set_ctldata(spi, cs);
  402. }
  403. mpc8xxx_spi = spi_master_get_devdata(spi->master);
  404. reg_base = mpc8xxx_spi->reg_base;
  405. pm_runtime_get_sync(mpc8xxx_spi->dev);
  406. hw_mode = cs->hw_mode; /* Save original settings */
  407. cs->hw_mode = mpc8xxx_spi_read_reg(
  408. &reg_base->csmode[spi->chip_select]);
  409. /* mask out bits we are going to set */
  410. cs->hw_mode &= ~(CSMODE_CP_BEGIN_EDGECLK | CSMODE_CI_INACTIVEHIGH
  411. | CSMODE_REV);
  412. if (spi->mode & SPI_CPHA)
  413. cs->hw_mode |= CSMODE_CP_BEGIN_EDGECLK;
  414. if (spi->mode & SPI_CPOL)
  415. cs->hw_mode |= CSMODE_CI_INACTIVEHIGH;
  416. if (!(spi->mode & SPI_LSB_FIRST))
  417. cs->hw_mode |= CSMODE_REV;
  418. /* Handle the loop mode */
  419. loop_mode = mpc8xxx_spi_read_reg(&reg_base->mode);
  420. loop_mode &= ~SPMODE_LOOP;
  421. if (spi->mode & SPI_LOOP)
  422. loop_mode |= SPMODE_LOOP;
  423. mpc8xxx_spi_write_reg(&reg_base->mode, loop_mode);
  424. retval = fsl_espi_setup_transfer(spi, NULL);
  425. pm_runtime_mark_last_busy(mpc8xxx_spi->dev);
  426. pm_runtime_put_autosuspend(mpc8xxx_spi->dev);
  427. if (retval < 0) {
  428. cs->hw_mode = hw_mode; /* Restore settings */
  429. return retval;
  430. }
  431. return 0;
  432. }
  433. static void fsl_espi_cleanup(struct spi_device *spi)
  434. {
  435. struct spi_mpc8xxx_cs *cs = spi_get_ctldata(spi);
  436. kfree(cs);
  437. spi_set_ctldata(spi, NULL);
  438. }
  439. void fsl_espi_cpu_irq(struct mpc8xxx_spi *mspi, u32 events)
  440. {
  441. struct fsl_espi_reg *reg_base = mspi->reg_base;
  442. /* We need handle RX first */
  443. if (events & SPIE_NE) {
  444. u32 rx_data, tmp;
  445. u8 rx_data_8;
  446. /* Spin until RX is done */
  447. while (SPIE_RXCNT(events) < min(4, mspi->len)) {
  448. cpu_relax();
  449. events = mpc8xxx_spi_read_reg(&reg_base->event);
  450. }
  451. if (mspi->len >= 4) {
  452. rx_data = mpc8xxx_spi_read_reg(&reg_base->receive);
  453. } else {
  454. tmp = mspi->len;
  455. rx_data = 0;
  456. while (tmp--) {
  457. rx_data_8 = in_8((u8 *)&reg_base->receive);
  458. rx_data |= (rx_data_8 << (tmp * 8));
  459. }
  460. rx_data <<= (4 - mspi->len) * 8;
  461. }
  462. mspi->len -= 4;
  463. if (mspi->rx)
  464. mspi->get_rx(rx_data, mspi);
  465. }
  466. if (!(events & SPIE_NF)) {
  467. int ret;
  468. /* spin until TX is done */
  469. ret = spin_event_timeout(((events = mpc8xxx_spi_read_reg(
  470. &reg_base->event)) & SPIE_NF), 1000, 0);
  471. if (!ret) {
  472. dev_err(mspi->dev, "tired waiting for SPIE_NF\n");
  473. /* Clear the SPIE bits */
  474. mpc8xxx_spi_write_reg(&reg_base->event, events);
  475. complete(&mspi->done);
  476. return;
  477. }
  478. }
  479. /* Clear the events */
  480. mpc8xxx_spi_write_reg(&reg_base->event, events);
  481. mspi->count -= 1;
  482. if (mspi->count) {
  483. u32 word = mspi->get_tx(mspi);
  484. mpc8xxx_spi_write_reg(&reg_base->transmit, word);
  485. } else {
  486. complete(&mspi->done);
  487. }
  488. }
  489. static irqreturn_t fsl_espi_irq(s32 irq, void *context_data)
  490. {
  491. struct mpc8xxx_spi *mspi = context_data;
  492. struct fsl_espi_reg *reg_base = mspi->reg_base;
  493. irqreturn_t ret = IRQ_NONE;
  494. u32 events;
  495. /* Get interrupt events(tx/rx) */
  496. events = mpc8xxx_spi_read_reg(&reg_base->event);
  497. if (events)
  498. ret = IRQ_HANDLED;
  499. dev_vdbg(mspi->dev, "%s: events %x\n", __func__, events);
  500. fsl_espi_cpu_irq(mspi, events);
  501. return ret;
  502. }
  503. #ifdef CONFIG_PM
  504. static int fsl_espi_runtime_suspend(struct device *dev)
  505. {
  506. struct spi_master *master = dev_get_drvdata(dev);
  507. struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(master);
  508. struct fsl_espi_reg *reg_base = mpc8xxx_spi->reg_base;
  509. u32 regval;
  510. regval = mpc8xxx_spi_read_reg(&reg_base->mode);
  511. regval &= ~SPMODE_ENABLE;
  512. mpc8xxx_spi_write_reg(&reg_base->mode, regval);
  513. return 0;
  514. }
  515. static int fsl_espi_runtime_resume(struct device *dev)
  516. {
  517. struct spi_master *master = dev_get_drvdata(dev);
  518. struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(master);
  519. struct fsl_espi_reg *reg_base = mpc8xxx_spi->reg_base;
  520. u32 regval;
  521. regval = mpc8xxx_spi_read_reg(&reg_base->mode);
  522. regval |= SPMODE_ENABLE;
  523. mpc8xxx_spi_write_reg(&reg_base->mode, regval);
  524. return 0;
  525. }
  526. #endif
  527. static struct spi_master * fsl_espi_probe(struct device *dev,
  528. struct resource *mem, unsigned int irq)
  529. {
  530. struct fsl_spi_platform_data *pdata = dev_get_platdata(dev);
  531. struct spi_master *master;
  532. struct mpc8xxx_spi *mpc8xxx_spi;
  533. struct fsl_espi_reg *reg_base;
  534. struct device_node *nc;
  535. const __be32 *prop;
  536. u32 regval, csmode;
  537. int i, len, ret = 0;
  538. master = spi_alloc_master(dev, sizeof(struct mpc8xxx_spi));
  539. if (!master) {
  540. ret = -ENOMEM;
  541. goto err;
  542. }
  543. dev_set_drvdata(dev, master);
  544. mpc8xxx_spi_probe(dev, mem, irq);
  545. master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 16);
  546. master->setup = fsl_espi_setup;
  547. master->cleanup = fsl_espi_cleanup;
  548. master->transfer_one_message = fsl_espi_do_one_msg;
  549. master->auto_runtime_pm = true;
  550. mpc8xxx_spi = spi_master_get_devdata(master);
  551. mpc8xxx_spi->reg_base = devm_ioremap_resource(dev, mem);
  552. if (IS_ERR(mpc8xxx_spi->reg_base)) {
  553. ret = PTR_ERR(mpc8xxx_spi->reg_base);
  554. goto err_probe;
  555. }
  556. reg_base = mpc8xxx_spi->reg_base;
  557. /* Register for SPI Interrupt */
  558. ret = devm_request_irq(dev, mpc8xxx_spi->irq, fsl_espi_irq,
  559. 0, "fsl_espi", mpc8xxx_spi);
  560. if (ret)
  561. goto err_probe;
  562. if (mpc8xxx_spi->flags & SPI_QE_CPU_MODE) {
  563. mpc8xxx_spi->rx_shift = 16;
  564. mpc8xxx_spi->tx_shift = 24;
  565. }
  566. /* SPI controller initializations */
  567. mpc8xxx_spi_write_reg(&reg_base->mode, 0);
  568. mpc8xxx_spi_write_reg(&reg_base->mask, 0);
  569. mpc8xxx_spi_write_reg(&reg_base->command, 0);
  570. mpc8xxx_spi_write_reg(&reg_base->event, 0xffffffff);
  571. /* Init eSPI CS mode register */
  572. for_each_available_child_of_node(master->dev.of_node, nc) {
  573. /* get chip select */
  574. prop = of_get_property(nc, "reg", &len);
  575. if (!prop || len < sizeof(*prop))
  576. continue;
  577. i = be32_to_cpup(prop);
  578. if (i < 0 || i >= pdata->max_chipselect)
  579. continue;
  580. csmode = CSMODE_INIT_VAL;
  581. /* check if CSBEF is set in device tree */
  582. prop = of_get_property(nc, "fsl,csbef", &len);
  583. if (prop && len >= sizeof(*prop)) {
  584. csmode &= ~(CSMODE_BEF(0xf));
  585. csmode |= CSMODE_BEF(be32_to_cpup(prop));
  586. }
  587. /* check if CSAFT is set in device tree */
  588. prop = of_get_property(nc, "fsl,csaft", &len);
  589. if (prop && len >= sizeof(*prop)) {
  590. csmode &= ~(CSMODE_AFT(0xf));
  591. csmode |= CSMODE_AFT(be32_to_cpup(prop));
  592. }
  593. mpc8xxx_spi_write_reg(&reg_base->csmode[i], csmode);
  594. dev_info(dev, "cs=%d, init_csmode=0x%x\n", i, csmode);
  595. }
  596. /* Enable SPI interface */
  597. regval = pdata->initial_spmode | SPMODE_INIT_VAL | SPMODE_ENABLE;
  598. mpc8xxx_spi_write_reg(&reg_base->mode, regval);
  599. pm_runtime_set_autosuspend_delay(dev, AUTOSUSPEND_TIMEOUT);
  600. pm_runtime_use_autosuspend(dev);
  601. pm_runtime_set_active(dev);
  602. pm_runtime_enable(dev);
  603. pm_runtime_get_sync(dev);
  604. ret = devm_spi_register_master(dev, master);
  605. if (ret < 0)
  606. goto err_pm;
  607. dev_info(dev, "at 0x%p (irq = %d)\n", reg_base, mpc8xxx_spi->irq);
  608. pm_runtime_mark_last_busy(dev);
  609. pm_runtime_put_autosuspend(dev);
  610. return master;
  611. err_pm:
  612. pm_runtime_put_noidle(dev);
  613. pm_runtime_disable(dev);
  614. pm_runtime_set_suspended(dev);
  615. err_probe:
  616. spi_master_put(master);
  617. err:
  618. return ERR_PTR(ret);
  619. }
  620. static int of_fsl_espi_get_chipselects(struct device *dev)
  621. {
  622. struct device_node *np = dev->of_node;
  623. struct fsl_spi_platform_data *pdata = dev_get_platdata(dev);
  624. const u32 *prop;
  625. int len;
  626. prop = of_get_property(np, "fsl,espi-num-chipselects", &len);
  627. if (!prop || len < sizeof(*prop)) {
  628. dev_err(dev, "No 'fsl,espi-num-chipselects' property\n");
  629. return -EINVAL;
  630. }
  631. pdata->max_chipselect = *prop;
  632. pdata->cs_control = NULL;
  633. return 0;
  634. }
  635. static int of_fsl_espi_probe(struct platform_device *ofdev)
  636. {
  637. struct device *dev = &ofdev->dev;
  638. struct device_node *np = ofdev->dev.of_node;
  639. struct spi_master *master;
  640. struct resource mem;
  641. unsigned int irq;
  642. int ret = -ENOMEM;
  643. ret = of_mpc8xxx_spi_probe(ofdev);
  644. if (ret)
  645. return ret;
  646. ret = of_fsl_espi_get_chipselects(dev);
  647. if (ret)
  648. goto err;
  649. ret = of_address_to_resource(np, 0, &mem);
  650. if (ret)
  651. goto err;
  652. irq = irq_of_parse_and_map(np, 0);
  653. if (!irq) {
  654. ret = -EINVAL;
  655. goto err;
  656. }
  657. master = fsl_espi_probe(dev, &mem, irq);
  658. if (IS_ERR(master)) {
  659. ret = PTR_ERR(master);
  660. goto err;
  661. }
  662. return 0;
  663. err:
  664. return ret;
  665. }
  666. static int of_fsl_espi_remove(struct platform_device *dev)
  667. {
  668. pm_runtime_disable(&dev->dev);
  669. return 0;
  670. }
  671. #ifdef CONFIG_PM_SLEEP
  672. static int of_fsl_espi_suspend(struct device *dev)
  673. {
  674. struct spi_master *master = dev_get_drvdata(dev);
  675. int ret;
  676. ret = spi_master_suspend(master);
  677. if (ret) {
  678. dev_warn(dev, "cannot suspend master\n");
  679. return ret;
  680. }
  681. ret = pm_runtime_force_suspend(dev);
  682. if (ret < 0)
  683. return ret;
  684. return 0;
  685. }
  686. static int of_fsl_espi_resume(struct device *dev)
  687. {
  688. struct fsl_spi_platform_data *pdata = dev_get_platdata(dev);
  689. struct spi_master *master = dev_get_drvdata(dev);
  690. struct mpc8xxx_spi *mpc8xxx_spi;
  691. struct fsl_espi_reg *reg_base;
  692. u32 regval;
  693. int i, ret;
  694. mpc8xxx_spi = spi_master_get_devdata(master);
  695. reg_base = mpc8xxx_spi->reg_base;
  696. /* SPI controller initializations */
  697. mpc8xxx_spi_write_reg(&reg_base->mode, 0);
  698. mpc8xxx_spi_write_reg(&reg_base->mask, 0);
  699. mpc8xxx_spi_write_reg(&reg_base->command, 0);
  700. mpc8xxx_spi_write_reg(&reg_base->event, 0xffffffff);
  701. /* Init eSPI CS mode register */
  702. for (i = 0; i < pdata->max_chipselect; i++)
  703. mpc8xxx_spi_write_reg(&reg_base->csmode[i], CSMODE_INIT_VAL);
  704. /* Enable SPI interface */
  705. regval = pdata->initial_spmode | SPMODE_INIT_VAL | SPMODE_ENABLE;
  706. mpc8xxx_spi_write_reg(&reg_base->mode, regval);
  707. ret = pm_runtime_force_resume(dev);
  708. if (ret < 0)
  709. return ret;
  710. return spi_master_resume(master);
  711. }
  712. #endif /* CONFIG_PM_SLEEP */
  713. static const struct dev_pm_ops espi_pm = {
  714. SET_RUNTIME_PM_OPS(fsl_espi_runtime_suspend,
  715. fsl_espi_runtime_resume, NULL)
  716. SET_SYSTEM_SLEEP_PM_OPS(of_fsl_espi_suspend, of_fsl_espi_resume)
  717. };
  718. static const struct of_device_id of_fsl_espi_match[] = {
  719. { .compatible = "fsl,mpc8536-espi" },
  720. {}
  721. };
  722. MODULE_DEVICE_TABLE(of, of_fsl_espi_match);
  723. static struct platform_driver fsl_espi_driver = {
  724. .driver = {
  725. .name = "fsl_espi",
  726. .of_match_table = of_fsl_espi_match,
  727. .pm = &espi_pm,
  728. },
  729. .probe = of_fsl_espi_probe,
  730. .remove = of_fsl_espi_remove,
  731. };
  732. module_platform_driver(fsl_espi_driver);
  733. MODULE_AUTHOR("Mingkai Hu");
  734. MODULE_DESCRIPTION("Enhanced Freescale SPI Driver");
  735. MODULE_LICENSE("GPL");