spi_mpc83xx.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  1. /*
  2. * MPC83xx 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/completion.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/delay.h>
  20. #include <linux/irq.h>
  21. #include <linux/device.h>
  22. #include <linux/spi/spi.h>
  23. #include <linux/spi/spi_bitbang.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/fsl_devices.h>
  26. #include <asm/irq.h>
  27. #include <asm/io.h>
  28. /* SPI Controller registers */
  29. struct mpc83xx_spi_reg {
  30. u8 res1[0x20];
  31. __be32 mode;
  32. __be32 event;
  33. __be32 mask;
  34. __be32 command;
  35. __be32 transmit;
  36. __be32 receive;
  37. };
  38. /* SPI Controller mode register definitions */
  39. #define SPMODE_LOOP (1 << 30)
  40. #define SPMODE_CI_INACTIVEHIGH (1 << 29)
  41. #define SPMODE_CP_BEGIN_EDGECLK (1 << 28)
  42. #define SPMODE_DIV16 (1 << 27)
  43. #define SPMODE_REV (1 << 26)
  44. #define SPMODE_MS (1 << 25)
  45. #define SPMODE_ENABLE (1 << 24)
  46. #define SPMODE_LEN(x) ((x) << 20)
  47. #define SPMODE_PM(x) ((x) << 16)
  48. #define SPMODE_OP (1 << 14)
  49. #define SPMODE_CG(x) ((x) << 7)
  50. /*
  51. * Default for SPI Mode:
  52. * SPI MODE 0 (inactive low, phase middle, MSB, 8-bit length, slow clk
  53. */
  54. #define SPMODE_INIT_VAL (SPMODE_CI_INACTIVEHIGH | SPMODE_DIV16 | SPMODE_REV | \
  55. SPMODE_MS | SPMODE_LEN(7) | SPMODE_PM(0xf))
  56. /* SPIE register values */
  57. #define SPIE_NE 0x00000200 /* Not empty */
  58. #define SPIE_NF 0x00000100 /* Not full */
  59. /* SPIM register values */
  60. #define SPIM_NE 0x00000200 /* Not empty */
  61. #define SPIM_NF 0x00000100 /* Not full */
  62. /* SPI Controller driver's private data. */
  63. struct mpc83xx_spi {
  64. struct mpc83xx_spi_reg __iomem *base;
  65. /* rx & tx bufs from the spi_transfer */
  66. const void *tx;
  67. void *rx;
  68. /* functions to deal with different sized buffers */
  69. void (*get_rx) (u32 rx_data, struct mpc83xx_spi *);
  70. u32(*get_tx) (struct mpc83xx_spi *);
  71. unsigned int count;
  72. int irq;
  73. unsigned nsecs; /* (clock cycle time)/2 */
  74. u32 spibrg; /* SPIBRG input clock */
  75. u32 rx_shift; /* RX data reg shift when in qe mode */
  76. u32 tx_shift; /* TX data reg shift when in qe mode */
  77. bool qe_mode;
  78. void (*activate_cs) (u8 cs, u8 polarity);
  79. void (*deactivate_cs) (u8 cs, u8 polarity);
  80. u8 busy;
  81. struct workqueue_struct *workqueue;
  82. struct work_struct work;
  83. struct list_head queue;
  84. spinlock_t lock;
  85. struct completion done;
  86. };
  87. struct spi_mpc83xx_cs {
  88. /* functions to deal with different sized buffers */
  89. void (*get_rx) (u32 rx_data, struct mpc83xx_spi *);
  90. u32 (*get_tx) (struct mpc83xx_spi *);
  91. u32 rx_shift; /* RX data reg shift when in qe mode */
  92. u32 tx_shift; /* TX data reg shift when in qe mode */
  93. u32 hw_mode; /* Holds HW mode register settings */
  94. };
  95. static inline void mpc83xx_spi_write_reg(__be32 __iomem * reg, u32 val)
  96. {
  97. out_be32(reg, val);
  98. }
  99. static inline u32 mpc83xx_spi_read_reg(__be32 __iomem * reg)
  100. {
  101. return in_be32(reg);
  102. }
  103. #define MPC83XX_SPI_RX_BUF(type) \
  104. void mpc83xx_spi_rx_buf_##type(u32 data, struct mpc83xx_spi *mpc83xx_spi) \
  105. { \
  106. type * rx = mpc83xx_spi->rx; \
  107. *rx++ = (type)(data >> mpc83xx_spi->rx_shift); \
  108. mpc83xx_spi->rx = rx; \
  109. }
  110. #define MPC83XX_SPI_TX_BUF(type) \
  111. u32 mpc83xx_spi_tx_buf_##type(struct mpc83xx_spi *mpc83xx_spi) \
  112. { \
  113. u32 data; \
  114. const type * tx = mpc83xx_spi->tx; \
  115. if (!tx) \
  116. return 0; \
  117. data = *tx++ << mpc83xx_spi->tx_shift; \
  118. mpc83xx_spi->tx = tx; \
  119. return data; \
  120. }
  121. MPC83XX_SPI_RX_BUF(u8)
  122. MPC83XX_SPI_RX_BUF(u16)
  123. MPC83XX_SPI_RX_BUF(u32)
  124. MPC83XX_SPI_TX_BUF(u8)
  125. MPC83XX_SPI_TX_BUF(u16)
  126. MPC83XX_SPI_TX_BUF(u32)
  127. static void mpc83xx_spi_chipselect(struct spi_device *spi, int value)
  128. {
  129. struct mpc83xx_spi *mpc83xx_spi;
  130. u8 pol = spi->mode & SPI_CS_HIGH ? 1 : 0;
  131. struct spi_mpc83xx_cs *cs = spi->controller_state;
  132. mpc83xx_spi = spi_master_get_devdata(spi->master);
  133. if (value == BITBANG_CS_INACTIVE) {
  134. if (mpc83xx_spi->deactivate_cs)
  135. mpc83xx_spi->deactivate_cs(spi->chip_select, pol);
  136. }
  137. if (value == BITBANG_CS_ACTIVE) {
  138. u32 regval = mpc83xx_spi_read_reg(&mpc83xx_spi->base->mode);
  139. mpc83xx_spi->rx_shift = cs->rx_shift;
  140. mpc83xx_spi->tx_shift = cs->tx_shift;
  141. mpc83xx_spi->get_rx = cs->get_rx;
  142. mpc83xx_spi->get_tx = cs->get_tx;
  143. if (cs->hw_mode != regval) {
  144. unsigned long flags;
  145. void *tmp_ptr = &mpc83xx_spi->base->mode;
  146. regval = cs->hw_mode;
  147. /* Turn off IRQs locally to minimize time that
  148. * SPI is disabled
  149. */
  150. local_irq_save(flags);
  151. /* Turn off SPI unit prior changing mode */
  152. mpc83xx_spi_write_reg(tmp_ptr, regval & ~SPMODE_ENABLE);
  153. mpc83xx_spi_write_reg(tmp_ptr, regval);
  154. local_irq_restore(flags);
  155. }
  156. if (mpc83xx_spi->activate_cs)
  157. mpc83xx_spi->activate_cs(spi->chip_select, pol);
  158. }
  159. }
  160. static
  161. int mpc83xx_spi_setup_transfer(struct spi_device *spi, struct spi_transfer *t)
  162. {
  163. struct mpc83xx_spi *mpc83xx_spi;
  164. u32 regval;
  165. u8 bits_per_word, pm;
  166. u32 hz;
  167. struct spi_mpc83xx_cs *cs = spi->controller_state;
  168. mpc83xx_spi = spi_master_get_devdata(spi->master);
  169. if (t) {
  170. bits_per_word = t->bits_per_word;
  171. hz = t->speed_hz;
  172. } else {
  173. bits_per_word = 0;
  174. hz = 0;
  175. }
  176. /* spi_transfer level calls that work per-word */
  177. if (!bits_per_word)
  178. bits_per_word = spi->bits_per_word;
  179. /* Make sure its a bit width we support [4..16, 32] */
  180. if ((bits_per_word < 4)
  181. || ((bits_per_word > 16) && (bits_per_word != 32)))
  182. return -EINVAL;
  183. if (!hz)
  184. hz = spi->max_speed_hz;
  185. cs->rx_shift = 0;
  186. cs->tx_shift = 0;
  187. if (bits_per_word <= 8) {
  188. cs->get_rx = mpc83xx_spi_rx_buf_u8;
  189. cs->get_tx = mpc83xx_spi_tx_buf_u8;
  190. if (mpc83xx_spi->qe_mode) {
  191. cs->rx_shift = 16;
  192. cs->tx_shift = 24;
  193. }
  194. } else if (bits_per_word <= 16) {
  195. cs->get_rx = mpc83xx_spi_rx_buf_u16;
  196. cs->get_tx = mpc83xx_spi_tx_buf_u16;
  197. if (mpc83xx_spi->qe_mode) {
  198. cs->rx_shift = 16;
  199. cs->tx_shift = 16;
  200. }
  201. } else if (bits_per_word <= 32) {
  202. cs->get_rx = mpc83xx_spi_rx_buf_u32;
  203. cs->get_tx = mpc83xx_spi_tx_buf_u32;
  204. } else
  205. return -EINVAL;
  206. if (mpc83xx_spi->qe_mode && spi->mode & SPI_LSB_FIRST) {
  207. cs->tx_shift = 0;
  208. if (bits_per_word <= 8)
  209. cs->rx_shift = 8;
  210. else
  211. cs->rx_shift = 0;
  212. }
  213. mpc83xx_spi->rx_shift = cs->rx_shift;
  214. mpc83xx_spi->tx_shift = cs->tx_shift;
  215. mpc83xx_spi->get_rx = cs->get_rx;
  216. mpc83xx_spi->get_tx = cs->get_tx;
  217. if (bits_per_word == 32)
  218. bits_per_word = 0;
  219. else
  220. bits_per_word = bits_per_word - 1;
  221. /* mask out bits we are going to set */
  222. cs->hw_mode &= ~(SPMODE_LEN(0xF) | SPMODE_DIV16
  223. | SPMODE_PM(0xF));
  224. cs->hw_mode |= SPMODE_LEN(bits_per_word);
  225. if ((mpc83xx_spi->spibrg / hz) > 64) {
  226. cs->hw_mode |= SPMODE_DIV16;
  227. pm = mpc83xx_spi->spibrg / (hz * 64);
  228. if (pm > 16) {
  229. dev_err(&spi->dev, "Requested speed is too "
  230. "low: %d Hz. Will use %d Hz instead.\n",
  231. hz, mpc83xx_spi->spibrg / 1024);
  232. pm = 16;
  233. }
  234. } else
  235. pm = mpc83xx_spi->spibrg / (hz * 4);
  236. if (pm)
  237. pm--;
  238. cs->hw_mode |= SPMODE_PM(pm);
  239. regval = mpc83xx_spi_read_reg(&mpc83xx_spi->base->mode);
  240. if (cs->hw_mode != regval) {
  241. unsigned long flags;
  242. void *tmp_ptr = &mpc83xx_spi->base->mode;
  243. regval = cs->hw_mode;
  244. /* Turn off IRQs locally to minimize time
  245. * that SPI is disabled
  246. */
  247. local_irq_save(flags);
  248. /* Turn off SPI unit prior changing mode */
  249. mpc83xx_spi_write_reg(tmp_ptr, regval & ~SPMODE_ENABLE);
  250. mpc83xx_spi_write_reg(tmp_ptr, regval);
  251. local_irq_restore(flags);
  252. }
  253. return 0;
  254. }
  255. static int mpc83xx_spi_bufs(struct spi_device *spi, struct spi_transfer *t)
  256. {
  257. struct mpc83xx_spi *mpc83xx_spi;
  258. u32 word, len, bits_per_word;
  259. mpc83xx_spi = spi_master_get_devdata(spi->master);
  260. mpc83xx_spi->tx = t->tx_buf;
  261. mpc83xx_spi->rx = t->rx_buf;
  262. bits_per_word = spi->bits_per_word;
  263. if (t->bits_per_word)
  264. bits_per_word = t->bits_per_word;
  265. len = t->len;
  266. if (bits_per_word > 8) {
  267. /* invalid length? */
  268. if (len & 1)
  269. return -EINVAL;
  270. len /= 2;
  271. }
  272. if (bits_per_word > 16) {
  273. /* invalid length? */
  274. if (len & 1)
  275. return -EINVAL;
  276. len /= 2;
  277. }
  278. mpc83xx_spi->count = len;
  279. INIT_COMPLETION(mpc83xx_spi->done);
  280. /* enable rx ints */
  281. mpc83xx_spi_write_reg(&mpc83xx_spi->base->mask, SPIM_NE);
  282. /* transmit word */
  283. word = mpc83xx_spi->get_tx(mpc83xx_spi);
  284. mpc83xx_spi_write_reg(&mpc83xx_spi->base->transmit, word);
  285. wait_for_completion(&mpc83xx_spi->done);
  286. /* disable rx ints */
  287. mpc83xx_spi_write_reg(&mpc83xx_spi->base->mask, 0);
  288. return mpc83xx_spi->count;
  289. }
  290. static void mpc83xx_spi_work(struct work_struct *work)
  291. {
  292. struct mpc83xx_spi *mpc83xx_spi =
  293. container_of(work, struct mpc83xx_spi, work);
  294. spin_lock_irq(&mpc83xx_spi->lock);
  295. mpc83xx_spi->busy = 1;
  296. while (!list_empty(&mpc83xx_spi->queue)) {
  297. struct spi_message *m;
  298. struct spi_device *spi;
  299. struct spi_transfer *t = NULL;
  300. unsigned cs_change;
  301. int status, nsecs = 50;
  302. m = container_of(mpc83xx_spi->queue.next,
  303. struct spi_message, queue);
  304. list_del_init(&m->queue);
  305. spin_unlock_irq(&mpc83xx_spi->lock);
  306. spi = m->spi;
  307. cs_change = 1;
  308. status = 0;
  309. list_for_each_entry(t, &m->transfers, transfer_list) {
  310. if (t->bits_per_word || t->speed_hz) {
  311. /* Don't allow changes if CS is active */
  312. status = -EINVAL;
  313. if (cs_change)
  314. status = mpc83xx_spi_setup_transfer(spi, t);
  315. if (status < 0)
  316. break;
  317. }
  318. if (cs_change)
  319. mpc83xx_spi_chipselect(spi, BITBANG_CS_ACTIVE);
  320. cs_change = t->cs_change;
  321. if (t->len)
  322. status = mpc83xx_spi_bufs(spi, t);
  323. if (status) {
  324. status = -EMSGSIZE;
  325. break;
  326. }
  327. m->actual_length += t->len;
  328. if (t->delay_usecs)
  329. udelay(t->delay_usecs);
  330. if (cs_change) {
  331. ndelay(nsecs);
  332. mpc83xx_spi_chipselect(spi, BITBANG_CS_INACTIVE);
  333. ndelay(nsecs);
  334. }
  335. }
  336. m->status = status;
  337. m->complete(m->context);
  338. if (status || !cs_change) {
  339. ndelay(nsecs);
  340. mpc83xx_spi_chipselect(spi, BITBANG_CS_INACTIVE);
  341. }
  342. mpc83xx_spi_setup_transfer(spi, NULL);
  343. spin_lock_irq(&mpc83xx_spi->lock);
  344. }
  345. mpc83xx_spi->busy = 0;
  346. spin_unlock_irq(&mpc83xx_spi->lock);
  347. }
  348. /* the spi->mode bits understood by this driver: */
  349. #define MODEBITS (SPI_CPOL | SPI_CPHA | SPI_CS_HIGH \
  350. | SPI_LSB_FIRST | SPI_LOOP)
  351. static int mpc83xx_spi_setup(struct spi_device *spi)
  352. {
  353. struct mpc83xx_spi *mpc83xx_spi;
  354. int retval;
  355. u32 hw_mode;
  356. struct spi_mpc83xx_cs *cs = spi->controller_state;
  357. if (spi->mode & ~MODEBITS) {
  358. dev_dbg(&spi->dev, "setup: unsupported mode bits %x\n",
  359. spi->mode & ~MODEBITS);
  360. return -EINVAL;
  361. }
  362. if (!spi->max_speed_hz)
  363. return -EINVAL;
  364. if (!cs) {
  365. cs = kzalloc(sizeof *cs, GFP_KERNEL);
  366. if (!cs)
  367. return -ENOMEM;
  368. spi->controller_state = cs;
  369. }
  370. mpc83xx_spi = spi_master_get_devdata(spi->master);
  371. if (!spi->bits_per_word)
  372. spi->bits_per_word = 8;
  373. hw_mode = cs->hw_mode; /* Save orginal settings */
  374. cs->hw_mode = mpc83xx_spi_read_reg(&mpc83xx_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 = mpc83xx_spi_setup_transfer(spi, NULL);
  387. if (retval < 0) {
  388. cs->hw_mode = hw_mode; /* Restore settings */
  389. return retval;
  390. }
  391. dev_dbg(&spi->dev, "%s, mode %d, %u bits/w, %u Hz\n",
  392. __func__, spi->mode & (SPI_CPOL | SPI_CPHA),
  393. spi->bits_per_word, spi->max_speed_hz);
  394. #if 0 /* Don't think this is needed */
  395. /* NOTE we _need_ to call chipselect() early, ideally with adapter
  396. * setup, unless the hardware defaults cooperate to avoid confusion
  397. * between normal (active low) and inverted chipselects.
  398. */
  399. /* deselect chip (low or high) */
  400. spin_lock(&mpc83xx_spi->lock);
  401. if (!mpc83xx_spi->busy)
  402. mpc83xx_spi_chipselect(spi, BITBANG_CS_INACTIVE);
  403. spin_unlock(&mpc83xx_spi->lock);
  404. #endif
  405. return 0;
  406. }
  407. irqreturn_t mpc83xx_spi_irq(s32 irq, void *context_data)
  408. {
  409. struct mpc83xx_spi *mpc83xx_spi = context_data;
  410. u32 event;
  411. irqreturn_t ret = IRQ_NONE;
  412. /* Get interrupt events(tx/rx) */
  413. event = mpc83xx_spi_read_reg(&mpc83xx_spi->base->event);
  414. /* We need handle RX first */
  415. if (event & SPIE_NE) {
  416. u32 rx_data = mpc83xx_spi_read_reg(&mpc83xx_spi->base->receive);
  417. if (mpc83xx_spi->rx)
  418. mpc83xx_spi->get_rx(rx_data, mpc83xx_spi);
  419. ret = IRQ_HANDLED;
  420. }
  421. if ((event & SPIE_NF) == 0)
  422. /* spin until TX is done */
  423. while (((event =
  424. mpc83xx_spi_read_reg(&mpc83xx_spi->base->event)) &
  425. SPIE_NF) == 0)
  426. cpu_relax();
  427. mpc83xx_spi->count -= 1;
  428. if (mpc83xx_spi->count) {
  429. u32 word = mpc83xx_spi->get_tx(mpc83xx_spi);
  430. mpc83xx_spi_write_reg(&mpc83xx_spi->base->transmit, word);
  431. } else {
  432. complete(&mpc83xx_spi->done);
  433. }
  434. /* Clear the events */
  435. mpc83xx_spi_write_reg(&mpc83xx_spi->base->event, event);
  436. return ret;
  437. }
  438. static int mpc83xx_spi_transfer(struct spi_device *spi,
  439. struct spi_message *m)
  440. {
  441. struct mpc83xx_spi *mpc83xx_spi = spi_master_get_devdata(spi->master);
  442. unsigned long flags;
  443. m->actual_length = 0;
  444. m->status = -EINPROGRESS;
  445. spin_lock_irqsave(&mpc83xx_spi->lock, flags);
  446. list_add_tail(&m->queue, &mpc83xx_spi->queue);
  447. queue_work(mpc83xx_spi->workqueue, &mpc83xx_spi->work);
  448. spin_unlock_irqrestore(&mpc83xx_spi->lock, flags);
  449. return 0;
  450. }
  451. static void mpc83xx_spi_cleanup(struct spi_device *spi)
  452. {
  453. kfree(spi->controller_state);
  454. }
  455. static int __init mpc83xx_spi_probe(struct platform_device *dev)
  456. {
  457. struct spi_master *master;
  458. struct mpc83xx_spi *mpc83xx_spi;
  459. struct fsl_spi_platform_data *pdata;
  460. struct resource *r;
  461. u32 regval;
  462. int ret = 0;
  463. /* Get resources(memory, IRQ) associated with the device */
  464. master = spi_alloc_master(&dev->dev, sizeof(struct mpc83xx_spi));
  465. if (master == NULL) {
  466. ret = -ENOMEM;
  467. goto err;
  468. }
  469. platform_set_drvdata(dev, master);
  470. pdata = dev->dev.platform_data;
  471. if (pdata == NULL) {
  472. ret = -ENODEV;
  473. goto free_master;
  474. }
  475. r = platform_get_resource(dev, IORESOURCE_MEM, 0);
  476. if (r == NULL) {
  477. ret = -ENODEV;
  478. goto free_master;
  479. }
  480. master->setup = mpc83xx_spi_setup;
  481. master->transfer = mpc83xx_spi_transfer;
  482. master->cleanup = mpc83xx_spi_cleanup;
  483. mpc83xx_spi = spi_master_get_devdata(master);
  484. mpc83xx_spi->activate_cs = pdata->activate_cs;
  485. mpc83xx_spi->deactivate_cs = pdata->deactivate_cs;
  486. mpc83xx_spi->qe_mode = pdata->qe_mode;
  487. mpc83xx_spi->get_rx = mpc83xx_spi_rx_buf_u8;
  488. mpc83xx_spi->get_tx = mpc83xx_spi_tx_buf_u8;
  489. mpc83xx_spi->spibrg = pdata->sysclk;
  490. mpc83xx_spi->rx_shift = 0;
  491. mpc83xx_spi->tx_shift = 0;
  492. if (mpc83xx_spi->qe_mode) {
  493. mpc83xx_spi->rx_shift = 16;
  494. mpc83xx_spi->tx_shift = 24;
  495. }
  496. init_completion(&mpc83xx_spi->done);
  497. mpc83xx_spi->base = ioremap(r->start, r->end - r->start + 1);
  498. if (mpc83xx_spi->base == NULL) {
  499. ret = -ENOMEM;
  500. goto put_master;
  501. }
  502. mpc83xx_spi->irq = platform_get_irq(dev, 0);
  503. if (mpc83xx_spi->irq < 0) {
  504. ret = -ENXIO;
  505. goto unmap_io;
  506. }
  507. /* Register for SPI Interrupt */
  508. ret = request_irq(mpc83xx_spi->irq, mpc83xx_spi_irq,
  509. 0, "mpc83xx_spi", mpc83xx_spi);
  510. if (ret != 0)
  511. goto unmap_io;
  512. master->bus_num = pdata->bus_num;
  513. master->num_chipselect = pdata->max_chipselect;
  514. /* SPI controller initializations */
  515. mpc83xx_spi_write_reg(&mpc83xx_spi->base->mode, 0);
  516. mpc83xx_spi_write_reg(&mpc83xx_spi->base->mask, 0);
  517. mpc83xx_spi_write_reg(&mpc83xx_spi->base->command, 0);
  518. mpc83xx_spi_write_reg(&mpc83xx_spi->base->event, 0xffffffff);
  519. /* Enable SPI interface */
  520. regval = pdata->initial_spmode | SPMODE_INIT_VAL | SPMODE_ENABLE;
  521. if (pdata->qe_mode)
  522. regval |= SPMODE_OP;
  523. mpc83xx_spi_write_reg(&mpc83xx_spi->base->mode, regval);
  524. spin_lock_init(&mpc83xx_spi->lock);
  525. init_completion(&mpc83xx_spi->done);
  526. INIT_WORK(&mpc83xx_spi->work, mpc83xx_spi_work);
  527. INIT_LIST_HEAD(&mpc83xx_spi->queue);
  528. mpc83xx_spi->workqueue = create_singlethread_workqueue(
  529. master->dev.parent->bus_id);
  530. if (mpc83xx_spi->workqueue == NULL) {
  531. ret = -EBUSY;
  532. goto free_irq;
  533. }
  534. ret = spi_register_master(master);
  535. if (ret < 0)
  536. goto unreg_master;
  537. printk(KERN_INFO
  538. "%s: MPC83xx SPI Controller driver at 0x%p (irq = %d)\n",
  539. dev->dev.bus_id, mpc83xx_spi->base, mpc83xx_spi->irq);
  540. return ret;
  541. unreg_master:
  542. destroy_workqueue(mpc83xx_spi->workqueue);
  543. free_irq:
  544. free_irq(mpc83xx_spi->irq, mpc83xx_spi);
  545. unmap_io:
  546. iounmap(mpc83xx_spi->base);
  547. put_master:
  548. spi_master_put(master);
  549. free_master:
  550. kfree(master);
  551. err:
  552. return ret;
  553. }
  554. static int __exit mpc83xx_spi_remove(struct platform_device *dev)
  555. {
  556. struct mpc83xx_spi *mpc83xx_spi;
  557. struct spi_master *master;
  558. master = platform_get_drvdata(dev);
  559. mpc83xx_spi = spi_master_get_devdata(master);
  560. flush_workqueue(mpc83xx_spi->workqueue);
  561. destroy_workqueue(mpc83xx_spi->workqueue);
  562. spi_unregister_master(master);
  563. free_irq(mpc83xx_spi->irq, mpc83xx_spi);
  564. iounmap(mpc83xx_spi->base);
  565. return 0;
  566. }
  567. MODULE_ALIAS("platform:mpc83xx_spi");
  568. static struct platform_driver mpc83xx_spi_driver = {
  569. .remove = __exit_p(mpc83xx_spi_remove),
  570. .driver = {
  571. .name = "mpc83xx_spi",
  572. .owner = THIS_MODULE,
  573. },
  574. };
  575. static int __init mpc83xx_spi_init(void)
  576. {
  577. return platform_driver_probe(&mpc83xx_spi_driver, mpc83xx_spi_probe);
  578. }
  579. static void __exit mpc83xx_spi_exit(void)
  580. {
  581. platform_driver_unregister(&mpc83xx_spi_driver);
  582. }
  583. module_init(mpc83xx_spi_init);
  584. module_exit(mpc83xx_spi_exit);
  585. MODULE_AUTHOR("Kumar Gala");
  586. MODULE_DESCRIPTION("Simple MPC83xx SPI Driver");
  587. MODULE_LICENSE("GPL");