spi_mpc83xx.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703
  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. pm = mpc83xx_spi->spibrg / (hz * 64) - 1;
  227. if (pm > 0x0f) {
  228. dev_err(&spi->dev, "Requested speed is too "
  229. "low: %d Hz. Will use %d Hz instead.\n",
  230. hz, mpc83xx_spi->spibrg / 1024);
  231. pm = 0x0f;
  232. }
  233. cs->hw_mode |= SPMODE_PM(pm) | SPMODE_DIV16;
  234. } else {
  235. pm = mpc83xx_spi->spibrg / (hz * 4);
  236. if (pm)
  237. pm--;
  238. cs->hw_mode |= SPMODE_PM(pm);
  239. }
  240. regval = mpc83xx_spi_read_reg(&mpc83xx_spi->base->mode);
  241. if (cs->hw_mode != regval) {
  242. unsigned long flags;
  243. void *tmp_ptr = &mpc83xx_spi->base->mode;
  244. regval = cs->hw_mode;
  245. /* Turn off IRQs locally to minimize time
  246. * that SPI is disabled
  247. */
  248. local_irq_save(flags);
  249. /* Turn off SPI unit prior changing mode */
  250. mpc83xx_spi_write_reg(tmp_ptr, regval & ~SPMODE_ENABLE);
  251. mpc83xx_spi_write_reg(tmp_ptr, regval);
  252. local_irq_restore(flags);
  253. }
  254. return 0;
  255. }
  256. static int mpc83xx_spi_bufs(struct spi_device *spi, struct spi_transfer *t)
  257. {
  258. struct mpc83xx_spi *mpc83xx_spi;
  259. u32 word, len, bits_per_word;
  260. mpc83xx_spi = spi_master_get_devdata(spi->master);
  261. mpc83xx_spi->tx = t->tx_buf;
  262. mpc83xx_spi->rx = t->rx_buf;
  263. bits_per_word = spi->bits_per_word;
  264. if (t->bits_per_word)
  265. bits_per_word = t->bits_per_word;
  266. len = t->len;
  267. if (bits_per_word > 8)
  268. len /= 2;
  269. if (bits_per_word > 16)
  270. len /= 2;
  271. mpc83xx_spi->count = len;
  272. INIT_COMPLETION(mpc83xx_spi->done);
  273. /* enable rx ints */
  274. mpc83xx_spi_write_reg(&mpc83xx_spi->base->mask, SPIM_NE);
  275. /* transmit word */
  276. word = mpc83xx_spi->get_tx(mpc83xx_spi);
  277. mpc83xx_spi_write_reg(&mpc83xx_spi->base->transmit, word);
  278. wait_for_completion(&mpc83xx_spi->done);
  279. /* disable rx ints */
  280. mpc83xx_spi_write_reg(&mpc83xx_spi->base->mask, 0);
  281. return mpc83xx_spi->count;
  282. }
  283. static void mpc83xx_spi_work(struct work_struct *work)
  284. {
  285. struct mpc83xx_spi *mpc83xx_spi =
  286. container_of(work, struct mpc83xx_spi, work);
  287. spin_lock_irq(&mpc83xx_spi->lock);
  288. mpc83xx_spi->busy = 1;
  289. while (!list_empty(&mpc83xx_spi->queue)) {
  290. struct spi_message *m;
  291. struct spi_device *spi;
  292. struct spi_transfer *t = NULL;
  293. unsigned cs_change;
  294. int status, nsecs = 50;
  295. m = container_of(mpc83xx_spi->queue.next,
  296. struct spi_message, queue);
  297. list_del_init(&m->queue);
  298. spin_unlock_irq(&mpc83xx_spi->lock);
  299. spi = m->spi;
  300. cs_change = 1;
  301. status = 0;
  302. list_for_each_entry(t, &m->transfers, transfer_list) {
  303. if (t->bits_per_word || t->speed_hz) {
  304. /* Don't allow changes if CS is active */
  305. status = -EINVAL;
  306. if (cs_change)
  307. status = mpc83xx_spi_setup_transfer(spi, t);
  308. if (status < 0)
  309. break;
  310. }
  311. if (cs_change)
  312. mpc83xx_spi_chipselect(spi, BITBANG_CS_ACTIVE);
  313. cs_change = t->cs_change;
  314. if (t->len)
  315. status = mpc83xx_spi_bufs(spi, t);
  316. if (status) {
  317. status = -EMSGSIZE;
  318. break;
  319. }
  320. m->actual_length += t->len;
  321. if (t->delay_usecs)
  322. udelay(t->delay_usecs);
  323. if (cs_change) {
  324. ndelay(nsecs);
  325. mpc83xx_spi_chipselect(spi, BITBANG_CS_INACTIVE);
  326. ndelay(nsecs);
  327. }
  328. }
  329. m->status = status;
  330. m->complete(m->context);
  331. if (status || !cs_change) {
  332. ndelay(nsecs);
  333. mpc83xx_spi_chipselect(spi, BITBANG_CS_INACTIVE);
  334. }
  335. mpc83xx_spi_setup_transfer(spi, NULL);
  336. spin_lock_irq(&mpc83xx_spi->lock);
  337. }
  338. mpc83xx_spi->busy = 0;
  339. spin_unlock_irq(&mpc83xx_spi->lock);
  340. }
  341. /* the spi->mode bits understood by this driver: */
  342. #define MODEBITS (SPI_CPOL | SPI_CPHA | SPI_CS_HIGH \
  343. | SPI_LSB_FIRST | SPI_LOOP)
  344. static int mpc83xx_spi_setup(struct spi_device *spi)
  345. {
  346. struct mpc83xx_spi *mpc83xx_spi;
  347. int retval;
  348. u32 hw_mode;
  349. struct spi_mpc83xx_cs *cs = spi->controller_state;
  350. if (spi->mode & ~MODEBITS) {
  351. dev_dbg(&spi->dev, "setup: unsupported mode bits %x\n",
  352. spi->mode & ~MODEBITS);
  353. return -EINVAL;
  354. }
  355. if (!spi->max_speed_hz)
  356. return -EINVAL;
  357. if (!cs) {
  358. cs = kzalloc(sizeof *cs, GFP_KERNEL);
  359. if (!cs)
  360. return -ENOMEM;
  361. spi->controller_state = cs;
  362. }
  363. mpc83xx_spi = spi_master_get_devdata(spi->master);
  364. if (!spi->bits_per_word)
  365. spi->bits_per_word = 8;
  366. hw_mode = cs->hw_mode; /* Save orginal settings */
  367. cs->hw_mode = mpc83xx_spi_read_reg(&mpc83xx_spi->base->mode);
  368. /* mask out bits we are going to set */
  369. cs->hw_mode &= ~(SPMODE_CP_BEGIN_EDGECLK | SPMODE_CI_INACTIVEHIGH
  370. | SPMODE_REV | SPMODE_LOOP);
  371. if (spi->mode & SPI_CPHA)
  372. cs->hw_mode |= SPMODE_CP_BEGIN_EDGECLK;
  373. if (spi->mode & SPI_CPOL)
  374. cs->hw_mode |= SPMODE_CI_INACTIVEHIGH;
  375. if (!(spi->mode & SPI_LSB_FIRST))
  376. cs->hw_mode |= SPMODE_REV;
  377. if (spi->mode & SPI_LOOP)
  378. cs->hw_mode |= SPMODE_LOOP;
  379. retval = mpc83xx_spi_setup_transfer(spi, NULL);
  380. if (retval < 0) {
  381. cs->hw_mode = hw_mode; /* Restore settings */
  382. return retval;
  383. }
  384. dev_dbg(&spi->dev, "%s, mode %d, %u bits/w, %u Hz\n",
  385. __func__, spi->mode & (SPI_CPOL | SPI_CPHA),
  386. spi->bits_per_word, spi->max_speed_hz);
  387. #if 0 /* Don't think this is needed */
  388. /* NOTE we _need_ to call chipselect() early, ideally with adapter
  389. * setup, unless the hardware defaults cooperate to avoid confusion
  390. * between normal (active low) and inverted chipselects.
  391. */
  392. /* deselect chip (low or high) */
  393. spin_lock(&mpc83xx_spi->lock);
  394. if (!mpc83xx_spi->busy)
  395. mpc83xx_spi_chipselect(spi, BITBANG_CS_INACTIVE);
  396. spin_unlock(&mpc83xx_spi->lock);
  397. #endif
  398. return 0;
  399. }
  400. irqreturn_t mpc83xx_spi_irq(s32 irq, void *context_data)
  401. {
  402. struct mpc83xx_spi *mpc83xx_spi = context_data;
  403. u32 event;
  404. irqreturn_t ret = IRQ_NONE;
  405. /* Get interrupt events(tx/rx) */
  406. event = mpc83xx_spi_read_reg(&mpc83xx_spi->base->event);
  407. /* We need handle RX first */
  408. if (event & SPIE_NE) {
  409. u32 rx_data = mpc83xx_spi_read_reg(&mpc83xx_spi->base->receive);
  410. if (mpc83xx_spi->rx)
  411. mpc83xx_spi->get_rx(rx_data, mpc83xx_spi);
  412. ret = IRQ_HANDLED;
  413. }
  414. if ((event & SPIE_NF) == 0)
  415. /* spin until TX is done */
  416. while (((event =
  417. mpc83xx_spi_read_reg(&mpc83xx_spi->base->event)) &
  418. SPIE_NF) == 0)
  419. cpu_relax();
  420. mpc83xx_spi->count -= 1;
  421. if (mpc83xx_spi->count) {
  422. u32 word = mpc83xx_spi->get_tx(mpc83xx_spi);
  423. mpc83xx_spi_write_reg(&mpc83xx_spi->base->transmit, word);
  424. } else {
  425. complete(&mpc83xx_spi->done);
  426. }
  427. /* Clear the events */
  428. mpc83xx_spi_write_reg(&mpc83xx_spi->base->event, event);
  429. return ret;
  430. }
  431. static int mpc83xx_spi_transfer(struct spi_device *spi,
  432. struct spi_message *m)
  433. {
  434. struct mpc83xx_spi *mpc83xx_spi = spi_master_get_devdata(spi->master);
  435. unsigned long flags;
  436. m->actual_length = 0;
  437. m->status = -EINPROGRESS;
  438. spin_lock_irqsave(&mpc83xx_spi->lock, flags);
  439. list_add_tail(&m->queue, &mpc83xx_spi->queue);
  440. queue_work(mpc83xx_spi->workqueue, &mpc83xx_spi->work);
  441. spin_unlock_irqrestore(&mpc83xx_spi->lock, flags);
  442. return 0;
  443. }
  444. static void mpc83xx_spi_cleanup(struct spi_device *spi)
  445. {
  446. kfree(spi->controller_state);
  447. }
  448. static int __init mpc83xx_spi_probe(struct platform_device *dev)
  449. {
  450. struct spi_master *master;
  451. struct mpc83xx_spi *mpc83xx_spi;
  452. struct fsl_spi_platform_data *pdata;
  453. struct resource *r;
  454. u32 regval;
  455. int ret = 0;
  456. /* Get resources(memory, IRQ) associated with the device */
  457. master = spi_alloc_master(&dev->dev, sizeof(struct mpc83xx_spi));
  458. if (master == NULL) {
  459. ret = -ENOMEM;
  460. goto err;
  461. }
  462. platform_set_drvdata(dev, master);
  463. pdata = dev->dev.platform_data;
  464. if (pdata == NULL) {
  465. ret = -ENODEV;
  466. goto free_master;
  467. }
  468. r = platform_get_resource(dev, IORESOURCE_MEM, 0);
  469. if (r == NULL) {
  470. ret = -ENODEV;
  471. goto free_master;
  472. }
  473. master->setup = mpc83xx_spi_setup;
  474. master->transfer = mpc83xx_spi_transfer;
  475. master->cleanup = mpc83xx_spi_cleanup;
  476. mpc83xx_spi = spi_master_get_devdata(master);
  477. mpc83xx_spi->activate_cs = pdata->activate_cs;
  478. mpc83xx_spi->deactivate_cs = pdata->deactivate_cs;
  479. mpc83xx_spi->qe_mode = pdata->qe_mode;
  480. mpc83xx_spi->get_rx = mpc83xx_spi_rx_buf_u8;
  481. mpc83xx_spi->get_tx = mpc83xx_spi_tx_buf_u8;
  482. mpc83xx_spi->spibrg = pdata->sysclk;
  483. mpc83xx_spi->rx_shift = 0;
  484. mpc83xx_spi->tx_shift = 0;
  485. if (mpc83xx_spi->qe_mode) {
  486. mpc83xx_spi->rx_shift = 16;
  487. mpc83xx_spi->tx_shift = 24;
  488. }
  489. init_completion(&mpc83xx_spi->done);
  490. mpc83xx_spi->base = ioremap(r->start, r->end - r->start + 1);
  491. if (mpc83xx_spi->base == NULL) {
  492. ret = -ENOMEM;
  493. goto put_master;
  494. }
  495. mpc83xx_spi->irq = platform_get_irq(dev, 0);
  496. if (mpc83xx_spi->irq < 0) {
  497. ret = -ENXIO;
  498. goto unmap_io;
  499. }
  500. /* Register for SPI Interrupt */
  501. ret = request_irq(mpc83xx_spi->irq, mpc83xx_spi_irq,
  502. 0, "mpc83xx_spi", mpc83xx_spi);
  503. if (ret != 0)
  504. goto unmap_io;
  505. master->bus_num = pdata->bus_num;
  506. master->num_chipselect = pdata->max_chipselect;
  507. /* SPI controller initializations */
  508. mpc83xx_spi_write_reg(&mpc83xx_spi->base->mode, 0);
  509. mpc83xx_spi_write_reg(&mpc83xx_spi->base->mask, 0);
  510. mpc83xx_spi_write_reg(&mpc83xx_spi->base->command, 0);
  511. mpc83xx_spi_write_reg(&mpc83xx_spi->base->event, 0xffffffff);
  512. /* Enable SPI interface */
  513. regval = pdata->initial_spmode | SPMODE_INIT_VAL | SPMODE_ENABLE;
  514. if (pdata->qe_mode)
  515. regval |= SPMODE_OP;
  516. mpc83xx_spi_write_reg(&mpc83xx_spi->base->mode, regval);
  517. spin_lock_init(&mpc83xx_spi->lock);
  518. init_completion(&mpc83xx_spi->done);
  519. INIT_WORK(&mpc83xx_spi->work, mpc83xx_spi_work);
  520. INIT_LIST_HEAD(&mpc83xx_spi->queue);
  521. mpc83xx_spi->workqueue = create_singlethread_workqueue(
  522. master->dev.parent->bus_id);
  523. if (mpc83xx_spi->workqueue == NULL) {
  524. ret = -EBUSY;
  525. goto free_irq;
  526. }
  527. ret = spi_register_master(master);
  528. if (ret < 0)
  529. goto unreg_master;
  530. printk(KERN_INFO
  531. "%s: MPC83xx SPI Controller driver at 0x%p (irq = %d)\n",
  532. dev->dev.bus_id, mpc83xx_spi->base, mpc83xx_spi->irq);
  533. return ret;
  534. unreg_master:
  535. destroy_workqueue(mpc83xx_spi->workqueue);
  536. free_irq:
  537. free_irq(mpc83xx_spi->irq, mpc83xx_spi);
  538. unmap_io:
  539. iounmap(mpc83xx_spi->base);
  540. put_master:
  541. spi_master_put(master);
  542. free_master:
  543. kfree(master);
  544. err:
  545. return ret;
  546. }
  547. static int __exit mpc83xx_spi_remove(struct platform_device *dev)
  548. {
  549. struct mpc83xx_spi *mpc83xx_spi;
  550. struct spi_master *master;
  551. master = platform_get_drvdata(dev);
  552. mpc83xx_spi = spi_master_get_devdata(master);
  553. flush_workqueue(mpc83xx_spi->workqueue);
  554. destroy_workqueue(mpc83xx_spi->workqueue);
  555. spi_unregister_master(master);
  556. free_irq(mpc83xx_spi->irq, mpc83xx_spi);
  557. iounmap(mpc83xx_spi->base);
  558. return 0;
  559. }
  560. MODULE_ALIAS("platform:mpc83xx_spi");
  561. static struct platform_driver mpc83xx_spi_driver = {
  562. .remove = __exit_p(mpc83xx_spi_remove),
  563. .driver = {
  564. .name = "mpc83xx_spi",
  565. .owner = THIS_MODULE,
  566. },
  567. };
  568. static int __init mpc83xx_spi_init(void)
  569. {
  570. return platform_driver_probe(&mpc83xx_spi_driver, mpc83xx_spi_probe);
  571. }
  572. static void __exit mpc83xx_spi_exit(void)
  573. {
  574. platform_driver_unregister(&mpc83xx_spi_driver);
  575. }
  576. module_init(mpc83xx_spi_init);
  577. module_exit(mpc83xx_spi_exit);
  578. MODULE_AUTHOR("Kumar Gala");
  579. MODULE_DESCRIPTION("Simple MPC83xx SPI Driver");
  580. MODULE_LICENSE("GPL");