spi-fsl-espi.c 21 KB

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