spi-fsl-espi.c 18 KB

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