spi_mpc8xxx.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945
  1. /*
  2. * MPC8xxx SPI controller driver.
  3. *
  4. * Maintainer: Kumar Gala
  5. *
  6. * Copyright (C) 2006 Polycom, Inc.
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2 of the License, or (at your
  11. * option) any later version.
  12. */
  13. #include <linux/module.h>
  14. #include <linux/init.h>
  15. #include <linux/types.h>
  16. #include <linux/kernel.h>
  17. #include <linux/bug.h>
  18. #include <linux/errno.h>
  19. #include <linux/err.h>
  20. #include <linux/io.h>
  21. #include <linux/completion.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/delay.h>
  24. #include <linux/irq.h>
  25. #include <linux/device.h>
  26. #include <linux/spi/spi.h>
  27. #include <linux/spi/spi_bitbang.h>
  28. #include <linux/platform_device.h>
  29. #include <linux/fsl_devices.h>
  30. #include <linux/of.h>
  31. #include <linux/of_platform.h>
  32. #include <linux/gpio.h>
  33. #include <linux/of_gpio.h>
  34. #include <linux/of_spi.h>
  35. #include <sysdev/fsl_soc.h>
  36. #include <asm/irq.h>
  37. /* SPI Controller registers */
  38. struct mpc8xxx_spi_reg {
  39. u8 res1[0x20];
  40. __be32 mode;
  41. __be32 event;
  42. __be32 mask;
  43. __be32 command;
  44. __be32 transmit;
  45. __be32 receive;
  46. };
  47. /* SPI Controller mode register definitions */
  48. #define SPMODE_LOOP (1 << 30)
  49. #define SPMODE_CI_INACTIVEHIGH (1 << 29)
  50. #define SPMODE_CP_BEGIN_EDGECLK (1 << 28)
  51. #define SPMODE_DIV16 (1 << 27)
  52. #define SPMODE_REV (1 << 26)
  53. #define SPMODE_MS (1 << 25)
  54. #define SPMODE_ENABLE (1 << 24)
  55. #define SPMODE_LEN(x) ((x) << 20)
  56. #define SPMODE_PM(x) ((x) << 16)
  57. #define SPMODE_OP (1 << 14)
  58. #define SPMODE_CG(x) ((x) << 7)
  59. /*
  60. * Default for SPI Mode:
  61. * SPI MODE 0 (inactive low, phase middle, MSB, 8-bit length, slow clk
  62. */
  63. #define SPMODE_INIT_VAL (SPMODE_CI_INACTIVEHIGH | SPMODE_DIV16 | SPMODE_REV | \
  64. SPMODE_MS | SPMODE_LEN(7) | SPMODE_PM(0xf))
  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. /* SPI Controller driver's private data. */
  72. struct mpc8xxx_spi {
  73. struct mpc8xxx_spi_reg __iomem *base;
  74. /* rx & tx bufs from the spi_transfer */
  75. const void *tx;
  76. void *rx;
  77. /* functions to deal with different sized buffers */
  78. void (*get_rx) (u32 rx_data, struct mpc8xxx_spi *);
  79. u32(*get_tx) (struct mpc8xxx_spi *);
  80. unsigned int count;
  81. unsigned int irq;
  82. unsigned nsecs; /* (clock cycle time)/2 */
  83. u32 spibrg; /* SPIBRG input clock */
  84. u32 rx_shift; /* RX data reg shift when in qe mode */
  85. u32 tx_shift; /* TX data reg shift when in qe mode */
  86. bool qe_mode;
  87. struct workqueue_struct *workqueue;
  88. struct work_struct work;
  89. struct list_head queue;
  90. spinlock_t lock;
  91. struct completion done;
  92. };
  93. struct spi_mpc8xxx_cs {
  94. /* functions to deal with different sized buffers */
  95. void (*get_rx) (u32 rx_data, struct mpc8xxx_spi *);
  96. u32 (*get_tx) (struct mpc8xxx_spi *);
  97. u32 rx_shift; /* RX data reg shift when in qe mode */
  98. u32 tx_shift; /* TX data reg shift when in qe mode */
  99. u32 hw_mode; /* Holds HW mode register settings */
  100. };
  101. static inline void mpc8xxx_spi_write_reg(__be32 __iomem *reg, u32 val)
  102. {
  103. out_be32(reg, val);
  104. }
  105. static inline u32 mpc8xxx_spi_read_reg(__be32 __iomem *reg)
  106. {
  107. return in_be32(reg);
  108. }
  109. #define MPC83XX_SPI_RX_BUF(type) \
  110. static \
  111. void mpc8xxx_spi_rx_buf_##type(u32 data, struct mpc8xxx_spi *mpc8xxx_spi) \
  112. { \
  113. type *rx = mpc8xxx_spi->rx; \
  114. *rx++ = (type)(data >> mpc8xxx_spi->rx_shift); \
  115. mpc8xxx_spi->rx = rx; \
  116. }
  117. #define MPC83XX_SPI_TX_BUF(type) \
  118. static \
  119. u32 mpc8xxx_spi_tx_buf_##type(struct mpc8xxx_spi *mpc8xxx_spi) \
  120. { \
  121. u32 data; \
  122. const type *tx = mpc8xxx_spi->tx; \
  123. if (!tx) \
  124. return 0; \
  125. data = *tx++ << mpc8xxx_spi->tx_shift; \
  126. mpc8xxx_spi->tx = tx; \
  127. return data; \
  128. }
  129. MPC83XX_SPI_RX_BUF(u8)
  130. MPC83XX_SPI_RX_BUF(u16)
  131. MPC83XX_SPI_RX_BUF(u32)
  132. MPC83XX_SPI_TX_BUF(u8)
  133. MPC83XX_SPI_TX_BUF(u16)
  134. MPC83XX_SPI_TX_BUF(u32)
  135. static void mpc8xxx_spi_chipselect(struct spi_device *spi, int value)
  136. {
  137. struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(spi->master);
  138. struct fsl_spi_platform_data *pdata = spi->dev.parent->platform_data;
  139. bool pol = spi->mode & SPI_CS_HIGH;
  140. struct spi_mpc8xxx_cs *cs = spi->controller_state;
  141. if (value == BITBANG_CS_INACTIVE) {
  142. if (pdata->cs_control)
  143. pdata->cs_control(spi, !pol);
  144. }
  145. if (value == BITBANG_CS_ACTIVE) {
  146. u32 regval = mpc8xxx_spi_read_reg(&mpc8xxx_spi->base->mode);
  147. mpc8xxx_spi->rx_shift = cs->rx_shift;
  148. mpc8xxx_spi->tx_shift = cs->tx_shift;
  149. mpc8xxx_spi->get_rx = cs->get_rx;
  150. mpc8xxx_spi->get_tx = cs->get_tx;
  151. if (cs->hw_mode != regval) {
  152. unsigned long flags;
  153. __be32 __iomem *mode = &mpc8xxx_spi->base->mode;
  154. regval = cs->hw_mode;
  155. /* Turn off IRQs locally to minimize time that
  156. * SPI is disabled
  157. */
  158. local_irq_save(flags);
  159. /* Turn off SPI unit prior changing mode */
  160. mpc8xxx_spi_write_reg(mode, regval & ~SPMODE_ENABLE);
  161. mpc8xxx_spi_write_reg(mode, regval);
  162. local_irq_restore(flags);
  163. }
  164. if (pdata->cs_control)
  165. pdata->cs_control(spi, pol);
  166. }
  167. }
  168. static
  169. int mpc8xxx_spi_setup_transfer(struct spi_device *spi, struct spi_transfer *t)
  170. {
  171. struct mpc8xxx_spi *mpc8xxx_spi;
  172. u32 regval;
  173. u8 bits_per_word, pm;
  174. u32 hz;
  175. struct spi_mpc8xxx_cs *cs = spi->controller_state;
  176. mpc8xxx_spi = spi_master_get_devdata(spi->master);
  177. if (t) {
  178. bits_per_word = t->bits_per_word;
  179. hz = t->speed_hz;
  180. } else {
  181. bits_per_word = 0;
  182. hz = 0;
  183. }
  184. /* spi_transfer level calls that work per-word */
  185. if (!bits_per_word)
  186. bits_per_word = spi->bits_per_word;
  187. /* Make sure its a bit width we support [4..16, 32] */
  188. if ((bits_per_word < 4)
  189. || ((bits_per_word > 16) && (bits_per_word != 32)))
  190. return -EINVAL;
  191. if (!hz)
  192. hz = spi->max_speed_hz;
  193. cs->rx_shift = 0;
  194. cs->tx_shift = 0;
  195. if (bits_per_word <= 8) {
  196. cs->get_rx = mpc8xxx_spi_rx_buf_u8;
  197. cs->get_tx = mpc8xxx_spi_tx_buf_u8;
  198. if (mpc8xxx_spi->qe_mode) {
  199. cs->rx_shift = 16;
  200. cs->tx_shift = 24;
  201. }
  202. } else if (bits_per_word <= 16) {
  203. cs->get_rx = mpc8xxx_spi_rx_buf_u16;
  204. cs->get_tx = mpc8xxx_spi_tx_buf_u16;
  205. if (mpc8xxx_spi->qe_mode) {
  206. cs->rx_shift = 16;
  207. cs->tx_shift = 16;
  208. }
  209. } else if (bits_per_word <= 32) {
  210. cs->get_rx = mpc8xxx_spi_rx_buf_u32;
  211. cs->get_tx = mpc8xxx_spi_tx_buf_u32;
  212. } else
  213. return -EINVAL;
  214. if (mpc8xxx_spi->qe_mode && spi->mode & SPI_LSB_FIRST) {
  215. cs->tx_shift = 0;
  216. if (bits_per_word <= 8)
  217. cs->rx_shift = 8;
  218. else
  219. cs->rx_shift = 0;
  220. }
  221. mpc8xxx_spi->rx_shift = cs->rx_shift;
  222. mpc8xxx_spi->tx_shift = cs->tx_shift;
  223. mpc8xxx_spi->get_rx = cs->get_rx;
  224. mpc8xxx_spi->get_tx = cs->get_tx;
  225. if (bits_per_word == 32)
  226. bits_per_word = 0;
  227. else
  228. bits_per_word = bits_per_word - 1;
  229. /* mask out bits we are going to set */
  230. cs->hw_mode &= ~(SPMODE_LEN(0xF) | SPMODE_DIV16
  231. | SPMODE_PM(0xF));
  232. cs->hw_mode |= SPMODE_LEN(bits_per_word);
  233. if ((mpc8xxx_spi->spibrg / hz) > 64) {
  234. cs->hw_mode |= SPMODE_DIV16;
  235. pm = mpc8xxx_spi->spibrg / (hz * 64);
  236. WARN_ONCE(pm > 16, "%s: Requested speed is too low: %d Hz. "
  237. "Will use %d Hz instead.\n", dev_name(&spi->dev),
  238. hz, mpc8xxx_spi->spibrg / 1024);
  239. if (pm > 16)
  240. pm = 16;
  241. } else
  242. pm = mpc8xxx_spi->spibrg / (hz * 4);
  243. if (pm)
  244. pm--;
  245. cs->hw_mode |= SPMODE_PM(pm);
  246. regval = mpc8xxx_spi_read_reg(&mpc8xxx_spi->base->mode);
  247. if (cs->hw_mode != regval) {
  248. unsigned long flags;
  249. __be32 __iomem *mode = &mpc8xxx_spi->base->mode;
  250. regval = cs->hw_mode;
  251. /* Turn off IRQs locally to minimize time
  252. * that SPI is disabled
  253. */
  254. local_irq_save(flags);
  255. /* Turn off SPI unit prior changing mode */
  256. mpc8xxx_spi_write_reg(mode, regval & ~SPMODE_ENABLE);
  257. mpc8xxx_spi_write_reg(mode, regval);
  258. local_irq_restore(flags);
  259. }
  260. return 0;
  261. }
  262. static int mpc8xxx_spi_bufs(struct spi_device *spi, struct spi_transfer *t)
  263. {
  264. struct mpc8xxx_spi *mpc8xxx_spi;
  265. u32 word, len, bits_per_word;
  266. mpc8xxx_spi = spi_master_get_devdata(spi->master);
  267. mpc8xxx_spi->tx = t->tx_buf;
  268. mpc8xxx_spi->rx = t->rx_buf;
  269. bits_per_word = spi->bits_per_word;
  270. if (t->bits_per_word)
  271. bits_per_word = t->bits_per_word;
  272. len = t->len;
  273. if (bits_per_word > 8) {
  274. /* invalid length? */
  275. if (len & 1)
  276. return -EINVAL;
  277. len /= 2;
  278. }
  279. if (bits_per_word > 16) {
  280. /* invalid length? */
  281. if (len & 1)
  282. return -EINVAL;
  283. len /= 2;
  284. }
  285. mpc8xxx_spi->count = len;
  286. INIT_COMPLETION(mpc8xxx_spi->done);
  287. /* enable rx ints */
  288. mpc8xxx_spi_write_reg(&mpc8xxx_spi->base->mask, SPIM_NE);
  289. /* transmit word */
  290. word = mpc8xxx_spi->get_tx(mpc8xxx_spi);
  291. mpc8xxx_spi_write_reg(&mpc8xxx_spi->base->transmit, word);
  292. wait_for_completion(&mpc8xxx_spi->done);
  293. /* disable rx ints */
  294. mpc8xxx_spi_write_reg(&mpc8xxx_spi->base->mask, 0);
  295. return mpc8xxx_spi->count;
  296. }
  297. static void mpc8xxx_spi_do_one_msg(struct spi_message *m)
  298. {
  299. struct spi_device *spi = m->spi;
  300. struct spi_transfer *t;
  301. unsigned int cs_change;
  302. const int nsecs = 50;
  303. int status;
  304. cs_change = 1;
  305. status = 0;
  306. list_for_each_entry(t, &m->transfers, transfer_list) {
  307. if (t->bits_per_word || t->speed_hz) {
  308. /* Don't allow changes if CS is active */
  309. status = -EINVAL;
  310. if (cs_change)
  311. status = mpc8xxx_spi_setup_transfer(spi, t);
  312. if (status < 0)
  313. break;
  314. }
  315. if (cs_change) {
  316. mpc8xxx_spi_chipselect(spi, BITBANG_CS_ACTIVE);
  317. ndelay(nsecs);
  318. }
  319. cs_change = t->cs_change;
  320. if (t->len)
  321. status = mpc8xxx_spi_bufs(spi, t);
  322. if (status) {
  323. status = -EMSGSIZE;
  324. break;
  325. }
  326. m->actual_length += t->len;
  327. if (t->delay_usecs)
  328. udelay(t->delay_usecs);
  329. if (cs_change) {
  330. ndelay(nsecs);
  331. mpc8xxx_spi_chipselect(spi, BITBANG_CS_INACTIVE);
  332. ndelay(nsecs);
  333. }
  334. }
  335. m->status = status;
  336. m->complete(m->context);
  337. if (status || !cs_change) {
  338. ndelay(nsecs);
  339. mpc8xxx_spi_chipselect(spi, BITBANG_CS_INACTIVE);
  340. }
  341. mpc8xxx_spi_setup_transfer(spi, NULL);
  342. }
  343. static void mpc8xxx_spi_work(struct work_struct *work)
  344. {
  345. struct mpc8xxx_spi *mpc8xxx_spi = container_of(work, struct mpc8xxx_spi,
  346. work);
  347. spin_lock_irq(&mpc8xxx_spi->lock);
  348. while (!list_empty(&mpc8xxx_spi->queue)) {
  349. struct spi_message *m = container_of(mpc8xxx_spi->queue.next,
  350. struct spi_message, queue);
  351. list_del_init(&m->queue);
  352. spin_unlock_irq(&mpc8xxx_spi->lock);
  353. mpc8xxx_spi_do_one_msg(m);
  354. spin_lock_irq(&mpc8xxx_spi->lock);
  355. }
  356. spin_unlock_irq(&mpc8xxx_spi->lock);
  357. }
  358. static int mpc8xxx_spi_setup(struct spi_device *spi)
  359. {
  360. struct mpc8xxx_spi *mpc8xxx_spi;
  361. int retval;
  362. u32 hw_mode;
  363. struct spi_mpc8xxx_cs *cs = spi->controller_state;
  364. if (!spi->max_speed_hz)
  365. return -EINVAL;
  366. if (!cs) {
  367. cs = kzalloc(sizeof *cs, GFP_KERNEL);
  368. if (!cs)
  369. return -ENOMEM;
  370. spi->controller_state = cs;
  371. }
  372. mpc8xxx_spi = spi_master_get_devdata(spi->master);
  373. hw_mode = cs->hw_mode; /* Save orginal settings */
  374. cs->hw_mode = mpc8xxx_spi_read_reg(&mpc8xxx_spi->base->mode);
  375. /* mask out bits we are going to set */
  376. cs->hw_mode &= ~(SPMODE_CP_BEGIN_EDGECLK | SPMODE_CI_INACTIVEHIGH
  377. | SPMODE_REV | SPMODE_LOOP);
  378. if (spi->mode & SPI_CPHA)
  379. cs->hw_mode |= SPMODE_CP_BEGIN_EDGECLK;
  380. if (spi->mode & SPI_CPOL)
  381. cs->hw_mode |= SPMODE_CI_INACTIVEHIGH;
  382. if (!(spi->mode & SPI_LSB_FIRST))
  383. cs->hw_mode |= SPMODE_REV;
  384. if (spi->mode & SPI_LOOP)
  385. cs->hw_mode |= SPMODE_LOOP;
  386. retval = mpc8xxx_spi_setup_transfer(spi, NULL);
  387. if (retval < 0) {
  388. cs->hw_mode = hw_mode; /* Restore settings */
  389. return retval;
  390. }
  391. return 0;
  392. }
  393. static irqreturn_t mpc8xxx_spi_irq(s32 irq, void *context_data)
  394. {
  395. struct mpc8xxx_spi *mpc8xxx_spi = context_data;
  396. u32 event;
  397. irqreturn_t ret = IRQ_NONE;
  398. /* Get interrupt events(tx/rx) */
  399. event = mpc8xxx_spi_read_reg(&mpc8xxx_spi->base->event);
  400. /* We need handle RX first */
  401. if (event & SPIE_NE) {
  402. u32 rx_data = mpc8xxx_spi_read_reg(&mpc8xxx_spi->base->receive);
  403. if (mpc8xxx_spi->rx)
  404. mpc8xxx_spi->get_rx(rx_data, mpc8xxx_spi);
  405. ret = IRQ_HANDLED;
  406. }
  407. if ((event & SPIE_NF) == 0)
  408. /* spin until TX is done */
  409. while (((event =
  410. mpc8xxx_spi_read_reg(&mpc8xxx_spi->base->event)) &
  411. SPIE_NF) == 0)
  412. cpu_relax();
  413. mpc8xxx_spi->count -= 1;
  414. if (mpc8xxx_spi->count) {
  415. u32 word = mpc8xxx_spi->get_tx(mpc8xxx_spi);
  416. mpc8xxx_spi_write_reg(&mpc8xxx_spi->base->transmit, word);
  417. } else {
  418. complete(&mpc8xxx_spi->done);
  419. }
  420. /* Clear the events */
  421. mpc8xxx_spi_write_reg(&mpc8xxx_spi->base->event, event);
  422. return ret;
  423. }
  424. static int mpc8xxx_spi_transfer(struct spi_device *spi,
  425. struct spi_message *m)
  426. {
  427. struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(spi->master);
  428. unsigned long flags;
  429. m->actual_length = 0;
  430. m->status = -EINPROGRESS;
  431. spin_lock_irqsave(&mpc8xxx_spi->lock, flags);
  432. list_add_tail(&m->queue, &mpc8xxx_spi->queue);
  433. queue_work(mpc8xxx_spi->workqueue, &mpc8xxx_spi->work);
  434. spin_unlock_irqrestore(&mpc8xxx_spi->lock, flags);
  435. return 0;
  436. }
  437. static void mpc8xxx_spi_cleanup(struct spi_device *spi)
  438. {
  439. kfree(spi->controller_state);
  440. }
  441. static struct spi_master * __devinit
  442. mpc8xxx_spi_probe(struct device *dev, struct resource *mem, unsigned int irq)
  443. {
  444. struct fsl_spi_platform_data *pdata = dev->platform_data;
  445. struct spi_master *master;
  446. struct mpc8xxx_spi *mpc8xxx_spi;
  447. u32 regval;
  448. int ret = 0;
  449. master = spi_alloc_master(dev, sizeof(struct mpc8xxx_spi));
  450. if (master == NULL) {
  451. ret = -ENOMEM;
  452. goto err;
  453. }
  454. dev_set_drvdata(dev, master);
  455. /* the spi->mode bits understood by this driver: */
  456. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH
  457. | SPI_LSB_FIRST | SPI_LOOP;
  458. master->setup = mpc8xxx_spi_setup;
  459. master->transfer = mpc8xxx_spi_transfer;
  460. master->cleanup = mpc8xxx_spi_cleanup;
  461. mpc8xxx_spi = spi_master_get_devdata(master);
  462. mpc8xxx_spi->qe_mode = pdata->qe_mode;
  463. mpc8xxx_spi->get_rx = mpc8xxx_spi_rx_buf_u8;
  464. mpc8xxx_spi->get_tx = mpc8xxx_spi_tx_buf_u8;
  465. mpc8xxx_spi->spibrg = pdata->sysclk;
  466. mpc8xxx_spi->rx_shift = 0;
  467. mpc8xxx_spi->tx_shift = 0;
  468. if (mpc8xxx_spi->qe_mode) {
  469. mpc8xxx_spi->rx_shift = 16;
  470. mpc8xxx_spi->tx_shift = 24;
  471. }
  472. init_completion(&mpc8xxx_spi->done);
  473. mpc8xxx_spi->base = ioremap(mem->start, mem->end - mem->start + 1);
  474. if (mpc8xxx_spi->base == NULL) {
  475. ret = -ENOMEM;
  476. goto put_master;
  477. }
  478. mpc8xxx_spi->irq = irq;
  479. /* Register for SPI Interrupt */
  480. ret = request_irq(mpc8xxx_spi->irq, mpc8xxx_spi_irq,
  481. 0, "mpc8xxx_spi", mpc8xxx_spi);
  482. if (ret != 0)
  483. goto unmap_io;
  484. master->bus_num = pdata->bus_num;
  485. master->num_chipselect = pdata->max_chipselect;
  486. /* SPI controller initializations */
  487. mpc8xxx_spi_write_reg(&mpc8xxx_spi->base->mode, 0);
  488. mpc8xxx_spi_write_reg(&mpc8xxx_spi->base->mask, 0);
  489. mpc8xxx_spi_write_reg(&mpc8xxx_spi->base->command, 0);
  490. mpc8xxx_spi_write_reg(&mpc8xxx_spi->base->event, 0xffffffff);
  491. /* Enable SPI interface */
  492. regval = pdata->initial_spmode | SPMODE_INIT_VAL | SPMODE_ENABLE;
  493. if (pdata->qe_mode)
  494. regval |= SPMODE_OP;
  495. mpc8xxx_spi_write_reg(&mpc8xxx_spi->base->mode, regval);
  496. spin_lock_init(&mpc8xxx_spi->lock);
  497. init_completion(&mpc8xxx_spi->done);
  498. INIT_WORK(&mpc8xxx_spi->work, mpc8xxx_spi_work);
  499. INIT_LIST_HEAD(&mpc8xxx_spi->queue);
  500. mpc8xxx_spi->workqueue = create_singlethread_workqueue(
  501. dev_name(master->dev.parent));
  502. if (mpc8xxx_spi->workqueue == NULL) {
  503. ret = -EBUSY;
  504. goto free_irq;
  505. }
  506. ret = spi_register_master(master);
  507. if (ret < 0)
  508. goto unreg_master;
  509. printk(KERN_INFO
  510. "%s: MPC8xxx SPI Controller driver at 0x%p (irq = %d)\n",
  511. dev_name(dev), mpc8xxx_spi->base, mpc8xxx_spi->irq);
  512. return master;
  513. unreg_master:
  514. destroy_workqueue(mpc8xxx_spi->workqueue);
  515. free_irq:
  516. free_irq(mpc8xxx_spi->irq, mpc8xxx_spi);
  517. unmap_io:
  518. iounmap(mpc8xxx_spi->base);
  519. put_master:
  520. spi_master_put(master);
  521. err:
  522. return ERR_PTR(ret);
  523. }
  524. static int __devexit mpc8xxx_spi_remove(struct device *dev)
  525. {
  526. struct mpc8xxx_spi *mpc8xxx_spi;
  527. struct spi_master *master;
  528. master = dev_get_drvdata(dev);
  529. mpc8xxx_spi = spi_master_get_devdata(master);
  530. flush_workqueue(mpc8xxx_spi->workqueue);
  531. destroy_workqueue(mpc8xxx_spi->workqueue);
  532. spi_unregister_master(master);
  533. free_irq(mpc8xxx_spi->irq, mpc8xxx_spi);
  534. iounmap(mpc8xxx_spi->base);
  535. return 0;
  536. }
  537. struct mpc8xxx_spi_probe_info {
  538. struct fsl_spi_platform_data pdata;
  539. int *gpios;
  540. bool *alow_flags;
  541. };
  542. static struct mpc8xxx_spi_probe_info *
  543. to_of_pinfo(struct fsl_spi_platform_data *pdata)
  544. {
  545. return container_of(pdata, struct mpc8xxx_spi_probe_info, pdata);
  546. }
  547. static void mpc8xxx_spi_cs_control(struct spi_device *spi, bool on)
  548. {
  549. struct device *dev = spi->dev.parent;
  550. struct mpc8xxx_spi_probe_info *pinfo = to_of_pinfo(dev->platform_data);
  551. u16 cs = spi->chip_select;
  552. int gpio = pinfo->gpios[cs];
  553. bool alow = pinfo->alow_flags[cs];
  554. gpio_set_value(gpio, on ^ alow);
  555. }
  556. static int of_mpc8xxx_spi_get_chipselects(struct device *dev)
  557. {
  558. struct device_node *np = dev_archdata_get_node(&dev->archdata);
  559. struct fsl_spi_platform_data *pdata = dev->platform_data;
  560. struct mpc8xxx_spi_probe_info *pinfo = to_of_pinfo(pdata);
  561. unsigned int ngpios;
  562. int i = 0;
  563. int ret;
  564. ngpios = of_gpio_count(np);
  565. if (!ngpios) {
  566. /*
  567. * SPI w/o chip-select line. One SPI device is still permitted
  568. * though.
  569. */
  570. pdata->max_chipselect = 1;
  571. return 0;
  572. }
  573. pinfo->gpios = kmalloc(ngpios * sizeof(*pinfo->gpios), GFP_KERNEL);
  574. if (!pinfo->gpios)
  575. return -ENOMEM;
  576. memset(pinfo->gpios, -1, ngpios * sizeof(*pinfo->gpios));
  577. pinfo->alow_flags = kzalloc(ngpios * sizeof(*pinfo->alow_flags),
  578. GFP_KERNEL);
  579. if (!pinfo->alow_flags) {
  580. ret = -ENOMEM;
  581. goto err_alloc_flags;
  582. }
  583. for (; i < ngpios; i++) {
  584. int gpio;
  585. enum of_gpio_flags flags;
  586. gpio = of_get_gpio_flags(np, i, &flags);
  587. if (!gpio_is_valid(gpio)) {
  588. dev_err(dev, "invalid gpio #%d: %d\n", i, gpio);
  589. goto err_loop;
  590. }
  591. ret = gpio_request(gpio, dev_name(dev));
  592. if (ret) {
  593. dev_err(dev, "can't request gpio #%d: %d\n", i, ret);
  594. goto err_loop;
  595. }
  596. pinfo->gpios[i] = gpio;
  597. pinfo->alow_flags[i] = flags & OF_GPIO_ACTIVE_LOW;
  598. ret = gpio_direction_output(pinfo->gpios[i],
  599. pinfo->alow_flags[i]);
  600. if (ret) {
  601. dev_err(dev, "can't set output direction for gpio "
  602. "#%d: %d\n", i, ret);
  603. goto err_loop;
  604. }
  605. }
  606. pdata->max_chipselect = ngpios;
  607. pdata->cs_control = mpc8xxx_spi_cs_control;
  608. return 0;
  609. err_loop:
  610. while (i >= 0) {
  611. if (gpio_is_valid(pinfo->gpios[i]))
  612. gpio_free(pinfo->gpios[i]);
  613. i--;
  614. }
  615. kfree(pinfo->alow_flags);
  616. pinfo->alow_flags = NULL;
  617. err_alloc_flags:
  618. kfree(pinfo->gpios);
  619. pinfo->gpios = NULL;
  620. return ret;
  621. }
  622. static int of_mpc8xxx_spi_free_chipselects(struct device *dev)
  623. {
  624. struct fsl_spi_platform_data *pdata = dev->platform_data;
  625. struct mpc8xxx_spi_probe_info *pinfo = to_of_pinfo(pdata);
  626. int i;
  627. if (!pinfo->gpios)
  628. return 0;
  629. for (i = 0; i < pdata->max_chipselect; i++) {
  630. if (gpio_is_valid(pinfo->gpios[i]))
  631. gpio_free(pinfo->gpios[i]);
  632. }
  633. kfree(pinfo->gpios);
  634. kfree(pinfo->alow_flags);
  635. return 0;
  636. }
  637. static int __devinit of_mpc8xxx_spi_probe(struct of_device *ofdev,
  638. const struct of_device_id *ofid)
  639. {
  640. struct device *dev = &ofdev->dev;
  641. struct device_node *np = ofdev->node;
  642. struct mpc8xxx_spi_probe_info *pinfo;
  643. struct fsl_spi_platform_data *pdata;
  644. struct spi_master *master;
  645. struct resource mem;
  646. struct resource irq;
  647. const void *prop;
  648. int ret = -ENOMEM;
  649. pinfo = kzalloc(sizeof(*pinfo), GFP_KERNEL);
  650. if (!pinfo)
  651. return -ENOMEM;
  652. pdata = &pinfo->pdata;
  653. dev->platform_data = pdata;
  654. /* Allocate bus num dynamically. */
  655. pdata->bus_num = -1;
  656. /* SPI controller is either clocked from QE or SoC clock. */
  657. pdata->sysclk = get_brgfreq();
  658. if (pdata->sysclk == -1) {
  659. pdata->sysclk = fsl_get_sys_freq();
  660. if (pdata->sysclk == -1) {
  661. ret = -ENODEV;
  662. goto err_clk;
  663. }
  664. }
  665. prop = of_get_property(np, "mode", NULL);
  666. if (prop && !strcmp(prop, "cpu-qe"))
  667. pdata->qe_mode = 1;
  668. ret = of_mpc8xxx_spi_get_chipselects(dev);
  669. if (ret)
  670. goto err;
  671. ret = of_address_to_resource(np, 0, &mem);
  672. if (ret)
  673. goto err;
  674. ret = of_irq_to_resource(np, 0, &irq);
  675. if (!ret) {
  676. ret = -EINVAL;
  677. goto err;
  678. }
  679. master = mpc8xxx_spi_probe(dev, &mem, irq.start);
  680. if (IS_ERR(master)) {
  681. ret = PTR_ERR(master);
  682. goto err;
  683. }
  684. of_register_spi_devices(master, np);
  685. return 0;
  686. err:
  687. of_mpc8xxx_spi_free_chipselects(dev);
  688. err_clk:
  689. kfree(pinfo);
  690. return ret;
  691. }
  692. static int __devexit of_mpc8xxx_spi_remove(struct of_device *ofdev)
  693. {
  694. int ret;
  695. ret = mpc8xxx_spi_remove(&ofdev->dev);
  696. if (ret)
  697. return ret;
  698. of_mpc8xxx_spi_free_chipselects(&ofdev->dev);
  699. return 0;
  700. }
  701. static const struct of_device_id of_mpc8xxx_spi_match[] = {
  702. { .compatible = "fsl,spi" },
  703. {},
  704. };
  705. MODULE_DEVICE_TABLE(of, of_mpc8xxx_spi_match);
  706. static struct of_platform_driver of_mpc8xxx_spi_driver = {
  707. .name = "mpc8xxx_spi",
  708. .match_table = of_mpc8xxx_spi_match,
  709. .probe = of_mpc8xxx_spi_probe,
  710. .remove = __devexit_p(of_mpc8xxx_spi_remove),
  711. };
  712. #ifdef CONFIG_MPC832x_RDB
  713. /*
  714. * XXX XXX XXX
  715. * This is "legacy" platform driver, was used by the MPC8323E-RDB boards
  716. * only. The driver should go away soon, since newer MPC8323E-RDB's device
  717. * tree can work with OpenFirmware driver. But for now we support old trees
  718. * as well.
  719. */
  720. static int __devinit plat_mpc8xxx_spi_probe(struct platform_device *pdev)
  721. {
  722. struct resource *mem;
  723. unsigned int irq;
  724. struct spi_master *master;
  725. if (!pdev->dev.platform_data)
  726. return -EINVAL;
  727. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  728. if (!mem)
  729. return -EINVAL;
  730. irq = platform_get_irq(pdev, 0);
  731. if (!irq)
  732. return -EINVAL;
  733. master = mpc8xxx_spi_probe(&pdev->dev, mem, irq);
  734. if (IS_ERR(master))
  735. return PTR_ERR(master);
  736. return 0;
  737. }
  738. static int __devexit plat_mpc8xxx_spi_remove(struct platform_device *pdev)
  739. {
  740. return mpc8xxx_spi_remove(&pdev->dev);
  741. }
  742. MODULE_ALIAS("platform:mpc8xxx_spi");
  743. static struct platform_driver mpc8xxx_spi_driver = {
  744. .probe = plat_mpc8xxx_spi_probe,
  745. .remove = __exit_p(plat_mpc8xxx_spi_remove),
  746. .driver = {
  747. .name = "mpc8xxx_spi",
  748. .owner = THIS_MODULE,
  749. },
  750. };
  751. static bool legacy_driver_failed;
  752. static void __init legacy_driver_register(void)
  753. {
  754. legacy_driver_failed = platform_driver_register(&mpc8xxx_spi_driver);
  755. }
  756. static void __exit legacy_driver_unregister(void)
  757. {
  758. if (legacy_driver_failed)
  759. return;
  760. platform_driver_unregister(&mpc8xxx_spi_driver);
  761. }
  762. #else
  763. static void __init legacy_driver_register(void) {}
  764. static void __exit legacy_driver_unregister(void) {}
  765. #endif /* CONFIG_MPC832x_RDB */
  766. static int __init mpc8xxx_spi_init(void)
  767. {
  768. legacy_driver_register();
  769. return of_register_platform_driver(&of_mpc8xxx_spi_driver);
  770. }
  771. static void __exit mpc8xxx_spi_exit(void)
  772. {
  773. of_unregister_platform_driver(&of_mpc8xxx_spi_driver);
  774. legacy_driver_unregister();
  775. }
  776. module_init(mpc8xxx_spi_init);
  777. module_exit(mpc8xxx_spi_exit);
  778. MODULE_AUTHOR("Kumar Gala");
  779. MODULE_DESCRIPTION("Simple MPC8xxx SPI Driver");
  780. MODULE_LICENSE("GPL");