spi_mpc8xxx.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401
  1. /*
  2. * MPC8xxx SPI controller driver.
  3. *
  4. * Maintainer: Kumar Gala
  5. *
  6. * Copyright (C) 2006 Polycom, Inc.
  7. *
  8. * CPM SPI and QE buffer descriptors mode support:
  9. * Copyright (c) 2009 MontaVista Software, Inc.
  10. * Author: Anton Vorontsov <avorontsov@ru.mvista.com>
  11. *
  12. * This program is free software; you can redistribute it and/or modify it
  13. * under the terms of the GNU General Public License as published by the
  14. * Free Software Foundation; either version 2 of the License, or (at your
  15. * option) any later version.
  16. */
  17. #include <linux/module.h>
  18. #include <linux/init.h>
  19. #include <linux/types.h>
  20. #include <linux/kernel.h>
  21. #include <linux/bug.h>
  22. #include <linux/errno.h>
  23. #include <linux/err.h>
  24. #include <linux/io.h>
  25. #include <linux/completion.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/delay.h>
  28. #include <linux/irq.h>
  29. #include <linux/device.h>
  30. #include <linux/spi/spi.h>
  31. #include <linux/spi/spi_bitbang.h>
  32. #include <linux/platform_device.h>
  33. #include <linux/fsl_devices.h>
  34. #include <linux/dma-mapping.h>
  35. #include <linux/mm.h>
  36. #include <linux/mutex.h>
  37. #include <linux/of.h>
  38. #include <linux/of_platform.h>
  39. #include <linux/gpio.h>
  40. #include <linux/of_gpio.h>
  41. #include <linux/of_spi.h>
  42. #include <sysdev/fsl_soc.h>
  43. #include <asm/cpm.h>
  44. #include <asm/qe.h>
  45. #include <asm/irq.h>
  46. /* CPM1 and CPM2 are mutually exclusive. */
  47. #ifdef CONFIG_CPM1
  48. #include <asm/cpm1.h>
  49. #define CPM_SPI_CMD mk_cr_cmd(CPM_CR_CH_SPI, 0)
  50. #else
  51. #include <asm/cpm2.h>
  52. #define CPM_SPI_CMD mk_cr_cmd(CPM_CR_SPI_PAGE, CPM_CR_SPI_SBLOCK, 0, 0)
  53. #endif
  54. /* SPI Controller registers */
  55. struct mpc8xxx_spi_reg {
  56. u8 res1[0x20];
  57. __be32 mode;
  58. __be32 event;
  59. __be32 mask;
  60. __be32 command;
  61. __be32 transmit;
  62. __be32 receive;
  63. };
  64. /* SPI Parameter RAM */
  65. struct spi_pram {
  66. __be16 rbase; /* Rx Buffer descriptor base address */
  67. __be16 tbase; /* Tx Buffer descriptor base address */
  68. u8 rfcr; /* Rx function code */
  69. u8 tfcr; /* Tx function code */
  70. __be16 mrblr; /* Max receive buffer length */
  71. __be32 rstate; /* Internal */
  72. __be32 rdp; /* Internal */
  73. __be16 rbptr; /* Internal */
  74. __be16 rbc; /* Internal */
  75. __be32 rxtmp; /* Internal */
  76. __be32 tstate; /* Internal */
  77. __be32 tdp; /* Internal */
  78. __be16 tbptr; /* Internal */
  79. __be16 tbc; /* Internal */
  80. __be32 txtmp; /* Internal */
  81. __be32 res; /* Tx temp. */
  82. __be16 rpbase; /* Relocation pointer (CPM1 only) */
  83. __be16 res1; /* Reserved */
  84. };
  85. /* SPI Controller mode register definitions */
  86. #define SPMODE_LOOP (1 << 30)
  87. #define SPMODE_CI_INACTIVEHIGH (1 << 29)
  88. #define SPMODE_CP_BEGIN_EDGECLK (1 << 28)
  89. #define SPMODE_DIV16 (1 << 27)
  90. #define SPMODE_REV (1 << 26)
  91. #define SPMODE_MS (1 << 25)
  92. #define SPMODE_ENABLE (1 << 24)
  93. #define SPMODE_LEN(x) ((x) << 20)
  94. #define SPMODE_PM(x) ((x) << 16)
  95. #define SPMODE_OP (1 << 14)
  96. #define SPMODE_CG(x) ((x) << 7)
  97. /*
  98. * Default for SPI Mode:
  99. * SPI MODE 0 (inactive low, phase middle, MSB, 8-bit length, slow clk
  100. */
  101. #define SPMODE_INIT_VAL (SPMODE_CI_INACTIVEHIGH | SPMODE_DIV16 | SPMODE_REV | \
  102. SPMODE_MS | SPMODE_LEN(7) | SPMODE_PM(0xf))
  103. /* SPIE register values */
  104. #define SPIE_NE 0x00000200 /* Not empty */
  105. #define SPIE_NF 0x00000100 /* Not full */
  106. /* SPIM register values */
  107. #define SPIM_NE 0x00000200 /* Not empty */
  108. #define SPIM_NF 0x00000100 /* Not full */
  109. #define SPIE_TXB 0x00000200 /* Last char is written to tx fifo */
  110. #define SPIE_RXB 0x00000100 /* Last char is written to rx buf */
  111. /* SPCOM register values */
  112. #define SPCOM_STR (1 << 23) /* Start transmit */
  113. #define SPI_PRAM_SIZE 0x100
  114. #define SPI_MRBLR ((unsigned int)PAGE_SIZE)
  115. /* SPI Controller driver's private data. */
  116. struct mpc8xxx_spi {
  117. struct device *dev;
  118. struct mpc8xxx_spi_reg __iomem *base;
  119. /* rx & tx bufs from the spi_transfer */
  120. const void *tx;
  121. void *rx;
  122. int subblock;
  123. struct spi_pram __iomem *pram;
  124. struct cpm_buf_desc __iomem *tx_bd;
  125. struct cpm_buf_desc __iomem *rx_bd;
  126. struct spi_transfer *xfer_in_progress;
  127. /* dma addresses for CPM transfers */
  128. dma_addr_t tx_dma;
  129. dma_addr_t rx_dma;
  130. bool map_tx_dma;
  131. bool map_rx_dma;
  132. dma_addr_t dma_dummy_tx;
  133. dma_addr_t dma_dummy_rx;
  134. /* functions to deal with different sized buffers */
  135. void (*get_rx) (u32 rx_data, struct mpc8xxx_spi *);
  136. u32(*get_tx) (struct mpc8xxx_spi *);
  137. unsigned int count;
  138. unsigned int irq;
  139. unsigned nsecs; /* (clock cycle time)/2 */
  140. u32 spibrg; /* SPIBRG input clock */
  141. u32 rx_shift; /* RX data reg shift when in qe mode */
  142. u32 tx_shift; /* TX data reg shift when in qe mode */
  143. unsigned int flags;
  144. struct workqueue_struct *workqueue;
  145. struct work_struct work;
  146. struct list_head queue;
  147. spinlock_t lock;
  148. struct completion done;
  149. };
  150. static void *mpc8xxx_dummy_rx;
  151. static DEFINE_MUTEX(mpc8xxx_dummy_rx_lock);
  152. static int mpc8xxx_dummy_rx_refcnt;
  153. struct spi_mpc8xxx_cs {
  154. /* functions to deal with different sized buffers */
  155. void (*get_rx) (u32 rx_data, struct mpc8xxx_spi *);
  156. u32 (*get_tx) (struct mpc8xxx_spi *);
  157. u32 rx_shift; /* RX data reg shift when in qe mode */
  158. u32 tx_shift; /* TX data reg shift when in qe mode */
  159. u32 hw_mode; /* Holds HW mode register settings */
  160. };
  161. static inline void mpc8xxx_spi_write_reg(__be32 __iomem *reg, u32 val)
  162. {
  163. out_be32(reg, val);
  164. }
  165. static inline u32 mpc8xxx_spi_read_reg(__be32 __iomem *reg)
  166. {
  167. return in_be32(reg);
  168. }
  169. #define MPC83XX_SPI_RX_BUF(type) \
  170. static \
  171. void mpc8xxx_spi_rx_buf_##type(u32 data, struct mpc8xxx_spi *mpc8xxx_spi) \
  172. { \
  173. type *rx = mpc8xxx_spi->rx; \
  174. *rx++ = (type)(data >> mpc8xxx_spi->rx_shift); \
  175. mpc8xxx_spi->rx = rx; \
  176. }
  177. #define MPC83XX_SPI_TX_BUF(type) \
  178. static \
  179. u32 mpc8xxx_spi_tx_buf_##type(struct mpc8xxx_spi *mpc8xxx_spi) \
  180. { \
  181. u32 data; \
  182. const type *tx = mpc8xxx_spi->tx; \
  183. if (!tx) \
  184. return 0; \
  185. data = *tx++ << mpc8xxx_spi->tx_shift; \
  186. mpc8xxx_spi->tx = tx; \
  187. return data; \
  188. }
  189. MPC83XX_SPI_RX_BUF(u8)
  190. MPC83XX_SPI_RX_BUF(u16)
  191. MPC83XX_SPI_RX_BUF(u32)
  192. MPC83XX_SPI_TX_BUF(u8)
  193. MPC83XX_SPI_TX_BUF(u16)
  194. MPC83XX_SPI_TX_BUF(u32)
  195. static void mpc8xxx_spi_change_mode(struct spi_device *spi)
  196. {
  197. struct mpc8xxx_spi *mspi = spi_master_get_devdata(spi->master);
  198. struct spi_mpc8xxx_cs *cs = spi->controller_state;
  199. __be32 __iomem *mode = &mspi->base->mode;
  200. unsigned long flags;
  201. if (cs->hw_mode == mpc8xxx_spi_read_reg(mode))
  202. return;
  203. /* Turn off IRQs locally to minimize time that SPI is disabled. */
  204. local_irq_save(flags);
  205. /* Turn off SPI unit prior changing mode */
  206. mpc8xxx_spi_write_reg(mode, cs->hw_mode & ~SPMODE_ENABLE);
  207. mpc8xxx_spi_write_reg(mode, cs->hw_mode);
  208. /* When in CPM mode, we need to reinit tx and rx. */
  209. if (mspi->flags & SPI_CPM_MODE) {
  210. if (mspi->flags & SPI_QE) {
  211. qe_issue_cmd(QE_INIT_TX_RX, mspi->subblock,
  212. QE_CR_PROTOCOL_UNSPECIFIED, 0);
  213. } else {
  214. cpm_command(CPM_SPI_CMD, CPM_CR_INIT_TRX);
  215. if (mspi->flags & SPI_CPM1) {
  216. out_be16(&mspi->pram->rbptr,
  217. in_be16(&mspi->pram->rbase));
  218. out_be16(&mspi->pram->tbptr,
  219. in_be16(&mspi->pram->tbase));
  220. }
  221. }
  222. }
  223. local_irq_restore(flags);
  224. }
  225. static void mpc8xxx_spi_chipselect(struct spi_device *spi, int value)
  226. {
  227. struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(spi->master);
  228. struct fsl_spi_platform_data *pdata = spi->dev.parent->platform_data;
  229. bool pol = spi->mode & SPI_CS_HIGH;
  230. struct spi_mpc8xxx_cs *cs = spi->controller_state;
  231. if (value == BITBANG_CS_INACTIVE) {
  232. if (pdata->cs_control)
  233. pdata->cs_control(spi, !pol);
  234. }
  235. if (value == BITBANG_CS_ACTIVE) {
  236. mpc8xxx_spi->rx_shift = cs->rx_shift;
  237. mpc8xxx_spi->tx_shift = cs->tx_shift;
  238. mpc8xxx_spi->get_rx = cs->get_rx;
  239. mpc8xxx_spi->get_tx = cs->get_tx;
  240. mpc8xxx_spi_change_mode(spi);
  241. if (pdata->cs_control)
  242. pdata->cs_control(spi, pol);
  243. }
  244. }
  245. static
  246. int mpc8xxx_spi_setup_transfer(struct spi_device *spi, struct spi_transfer *t)
  247. {
  248. struct mpc8xxx_spi *mpc8xxx_spi;
  249. u8 bits_per_word, pm;
  250. u32 hz;
  251. struct spi_mpc8xxx_cs *cs = spi->controller_state;
  252. mpc8xxx_spi = spi_master_get_devdata(spi->master);
  253. if (t) {
  254. bits_per_word = t->bits_per_word;
  255. hz = t->speed_hz;
  256. } else {
  257. bits_per_word = 0;
  258. hz = 0;
  259. }
  260. /* spi_transfer level calls that work per-word */
  261. if (!bits_per_word)
  262. bits_per_word = spi->bits_per_word;
  263. /* Make sure its a bit width we support [4..16, 32] */
  264. if ((bits_per_word < 4)
  265. || ((bits_per_word > 16) && (bits_per_word != 32)))
  266. return -EINVAL;
  267. if (!hz)
  268. hz = spi->max_speed_hz;
  269. cs->rx_shift = 0;
  270. cs->tx_shift = 0;
  271. if (bits_per_word <= 8) {
  272. cs->get_rx = mpc8xxx_spi_rx_buf_u8;
  273. cs->get_tx = mpc8xxx_spi_tx_buf_u8;
  274. if (mpc8xxx_spi->flags & SPI_QE_CPU_MODE) {
  275. cs->rx_shift = 16;
  276. cs->tx_shift = 24;
  277. }
  278. } else if (bits_per_word <= 16) {
  279. cs->get_rx = mpc8xxx_spi_rx_buf_u16;
  280. cs->get_tx = mpc8xxx_spi_tx_buf_u16;
  281. if (mpc8xxx_spi->flags & SPI_QE_CPU_MODE) {
  282. cs->rx_shift = 16;
  283. cs->tx_shift = 16;
  284. }
  285. } else if (bits_per_word <= 32) {
  286. cs->get_rx = mpc8xxx_spi_rx_buf_u32;
  287. cs->get_tx = mpc8xxx_spi_tx_buf_u32;
  288. } else
  289. return -EINVAL;
  290. if (mpc8xxx_spi->flags & SPI_QE_CPU_MODE &&
  291. spi->mode & SPI_LSB_FIRST) {
  292. cs->tx_shift = 0;
  293. if (bits_per_word <= 8)
  294. cs->rx_shift = 8;
  295. else
  296. cs->rx_shift = 0;
  297. }
  298. mpc8xxx_spi->rx_shift = cs->rx_shift;
  299. mpc8xxx_spi->tx_shift = cs->tx_shift;
  300. mpc8xxx_spi->get_rx = cs->get_rx;
  301. mpc8xxx_spi->get_tx = cs->get_tx;
  302. if (bits_per_word == 32)
  303. bits_per_word = 0;
  304. else
  305. bits_per_word = bits_per_word - 1;
  306. /* mask out bits we are going to set */
  307. cs->hw_mode &= ~(SPMODE_LEN(0xF) | SPMODE_DIV16
  308. | SPMODE_PM(0xF));
  309. cs->hw_mode |= SPMODE_LEN(bits_per_word);
  310. if ((mpc8xxx_spi->spibrg / hz) > 64) {
  311. cs->hw_mode |= SPMODE_DIV16;
  312. pm = mpc8xxx_spi->spibrg / (hz * 64);
  313. WARN_ONCE(pm > 16, "%s: Requested speed is too low: %d Hz. "
  314. "Will use %d Hz instead.\n", dev_name(&spi->dev),
  315. hz, mpc8xxx_spi->spibrg / 1024);
  316. if (pm > 16)
  317. pm = 16;
  318. } else
  319. pm = mpc8xxx_spi->spibrg / (hz * 4);
  320. if (pm)
  321. pm--;
  322. cs->hw_mode |= SPMODE_PM(pm);
  323. mpc8xxx_spi_change_mode(spi);
  324. return 0;
  325. }
  326. static void mpc8xxx_spi_cpm_bufs_start(struct mpc8xxx_spi *mspi)
  327. {
  328. struct cpm_buf_desc __iomem *tx_bd = mspi->tx_bd;
  329. struct cpm_buf_desc __iomem *rx_bd = mspi->rx_bd;
  330. unsigned int xfer_len = min(mspi->count, SPI_MRBLR);
  331. unsigned int xfer_ofs;
  332. xfer_ofs = mspi->xfer_in_progress->len - mspi->count;
  333. out_be32(&rx_bd->cbd_bufaddr, mspi->rx_dma + xfer_ofs);
  334. out_be16(&rx_bd->cbd_datlen, 0);
  335. out_be16(&rx_bd->cbd_sc, BD_SC_EMPTY | BD_SC_INTRPT | BD_SC_WRAP);
  336. out_be32(&tx_bd->cbd_bufaddr, mspi->tx_dma + xfer_ofs);
  337. out_be16(&tx_bd->cbd_datlen, xfer_len);
  338. out_be16(&tx_bd->cbd_sc, BD_SC_READY | BD_SC_INTRPT | BD_SC_WRAP |
  339. BD_SC_LAST);
  340. /* start transfer */
  341. mpc8xxx_spi_write_reg(&mspi->base->command, SPCOM_STR);
  342. }
  343. static int mpc8xxx_spi_cpm_bufs(struct mpc8xxx_spi *mspi,
  344. struct spi_transfer *t, bool is_dma_mapped)
  345. {
  346. struct device *dev = mspi->dev;
  347. if (is_dma_mapped) {
  348. mspi->map_tx_dma = 0;
  349. mspi->map_rx_dma = 0;
  350. } else {
  351. mspi->map_tx_dma = 1;
  352. mspi->map_rx_dma = 1;
  353. }
  354. if (!t->tx_buf) {
  355. mspi->tx_dma = mspi->dma_dummy_tx;
  356. mspi->map_tx_dma = 0;
  357. }
  358. if (!t->rx_buf) {
  359. mspi->rx_dma = mspi->dma_dummy_rx;
  360. mspi->map_rx_dma = 0;
  361. }
  362. if (mspi->map_tx_dma) {
  363. void *nonconst_tx = (void *)mspi->tx; /* shut up gcc */
  364. mspi->tx_dma = dma_map_single(dev, nonconst_tx, t->len,
  365. DMA_TO_DEVICE);
  366. if (dma_mapping_error(dev, mspi->tx_dma)) {
  367. dev_err(dev, "unable to map tx dma\n");
  368. return -ENOMEM;
  369. }
  370. } else {
  371. mspi->tx_dma = t->tx_dma;
  372. }
  373. if (mspi->map_rx_dma) {
  374. mspi->rx_dma = dma_map_single(dev, mspi->rx, t->len,
  375. DMA_FROM_DEVICE);
  376. if (dma_mapping_error(dev, mspi->rx_dma)) {
  377. dev_err(dev, "unable to map rx dma\n");
  378. goto err_rx_dma;
  379. }
  380. } else {
  381. mspi->rx_dma = t->rx_dma;
  382. }
  383. /* enable rx ints */
  384. mpc8xxx_spi_write_reg(&mspi->base->mask, SPIE_RXB);
  385. mspi->xfer_in_progress = t;
  386. mspi->count = t->len;
  387. /* start CPM transfers */
  388. mpc8xxx_spi_cpm_bufs_start(mspi);
  389. return 0;
  390. err_rx_dma:
  391. if (mspi->map_tx_dma)
  392. dma_unmap_single(dev, mspi->tx_dma, t->len, DMA_TO_DEVICE);
  393. return -ENOMEM;
  394. }
  395. static void mpc8xxx_spi_cpm_bufs_complete(struct mpc8xxx_spi *mspi)
  396. {
  397. struct device *dev = mspi->dev;
  398. struct spi_transfer *t = mspi->xfer_in_progress;
  399. if (mspi->map_tx_dma)
  400. dma_unmap_single(dev, mspi->tx_dma, t->len, DMA_TO_DEVICE);
  401. if (mspi->map_tx_dma)
  402. dma_unmap_single(dev, mspi->rx_dma, t->len, DMA_FROM_DEVICE);
  403. mspi->xfer_in_progress = NULL;
  404. }
  405. static int mpc8xxx_spi_cpu_bufs(struct mpc8xxx_spi *mspi,
  406. struct spi_transfer *t, unsigned int len)
  407. {
  408. u32 word;
  409. mspi->count = len;
  410. /* enable rx ints */
  411. mpc8xxx_spi_write_reg(&mspi->base->mask, SPIM_NE);
  412. /* transmit word */
  413. word = mspi->get_tx(mspi);
  414. mpc8xxx_spi_write_reg(&mspi->base->transmit, word);
  415. return 0;
  416. }
  417. static int mpc8xxx_spi_bufs(struct spi_device *spi, struct spi_transfer *t,
  418. bool is_dma_mapped)
  419. {
  420. struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(spi->master);
  421. unsigned int len = t->len;
  422. u8 bits_per_word;
  423. int ret;
  424. bits_per_word = spi->bits_per_word;
  425. if (t->bits_per_word)
  426. bits_per_word = t->bits_per_word;
  427. if (bits_per_word > 8) {
  428. /* invalid length? */
  429. if (len & 1)
  430. return -EINVAL;
  431. len /= 2;
  432. }
  433. if (bits_per_word > 16) {
  434. /* invalid length? */
  435. if (len & 1)
  436. return -EINVAL;
  437. len /= 2;
  438. }
  439. mpc8xxx_spi->tx = t->tx_buf;
  440. mpc8xxx_spi->rx = t->rx_buf;
  441. INIT_COMPLETION(mpc8xxx_spi->done);
  442. if (mpc8xxx_spi->flags & SPI_CPM_MODE)
  443. ret = mpc8xxx_spi_cpm_bufs(mpc8xxx_spi, t, is_dma_mapped);
  444. else
  445. ret = mpc8xxx_spi_cpu_bufs(mpc8xxx_spi, t, len);
  446. if (ret)
  447. return ret;
  448. wait_for_completion(&mpc8xxx_spi->done);
  449. /* disable rx ints */
  450. mpc8xxx_spi_write_reg(&mpc8xxx_spi->base->mask, 0);
  451. if (mpc8xxx_spi->flags & SPI_CPM_MODE)
  452. mpc8xxx_spi_cpm_bufs_complete(mpc8xxx_spi);
  453. return mpc8xxx_spi->count;
  454. }
  455. static void mpc8xxx_spi_do_one_msg(struct spi_message *m)
  456. {
  457. struct spi_device *spi = m->spi;
  458. struct spi_transfer *t;
  459. unsigned int cs_change;
  460. const int nsecs = 50;
  461. int status;
  462. cs_change = 1;
  463. status = 0;
  464. list_for_each_entry(t, &m->transfers, transfer_list) {
  465. if (t->bits_per_word || t->speed_hz) {
  466. /* Don't allow changes if CS is active */
  467. status = -EINVAL;
  468. if (cs_change)
  469. status = mpc8xxx_spi_setup_transfer(spi, t);
  470. if (status < 0)
  471. break;
  472. }
  473. if (cs_change) {
  474. mpc8xxx_spi_chipselect(spi, BITBANG_CS_ACTIVE);
  475. ndelay(nsecs);
  476. }
  477. cs_change = t->cs_change;
  478. if (t->len)
  479. status = mpc8xxx_spi_bufs(spi, t, m->is_dma_mapped);
  480. if (status) {
  481. status = -EMSGSIZE;
  482. break;
  483. }
  484. m->actual_length += t->len;
  485. if (t->delay_usecs)
  486. udelay(t->delay_usecs);
  487. if (cs_change) {
  488. ndelay(nsecs);
  489. mpc8xxx_spi_chipselect(spi, BITBANG_CS_INACTIVE);
  490. ndelay(nsecs);
  491. }
  492. }
  493. m->status = status;
  494. m->complete(m->context);
  495. if (status || !cs_change) {
  496. ndelay(nsecs);
  497. mpc8xxx_spi_chipselect(spi, BITBANG_CS_INACTIVE);
  498. }
  499. mpc8xxx_spi_setup_transfer(spi, NULL);
  500. }
  501. static void mpc8xxx_spi_work(struct work_struct *work)
  502. {
  503. struct mpc8xxx_spi *mpc8xxx_spi = container_of(work, struct mpc8xxx_spi,
  504. work);
  505. spin_lock_irq(&mpc8xxx_spi->lock);
  506. while (!list_empty(&mpc8xxx_spi->queue)) {
  507. struct spi_message *m = container_of(mpc8xxx_spi->queue.next,
  508. struct spi_message, queue);
  509. list_del_init(&m->queue);
  510. spin_unlock_irq(&mpc8xxx_spi->lock);
  511. mpc8xxx_spi_do_one_msg(m);
  512. spin_lock_irq(&mpc8xxx_spi->lock);
  513. }
  514. spin_unlock_irq(&mpc8xxx_spi->lock);
  515. }
  516. static int mpc8xxx_spi_setup(struct spi_device *spi)
  517. {
  518. struct mpc8xxx_spi *mpc8xxx_spi;
  519. int retval;
  520. u32 hw_mode;
  521. struct spi_mpc8xxx_cs *cs = spi->controller_state;
  522. if (!spi->max_speed_hz)
  523. return -EINVAL;
  524. if (!cs) {
  525. cs = kzalloc(sizeof *cs, GFP_KERNEL);
  526. if (!cs)
  527. return -ENOMEM;
  528. spi->controller_state = cs;
  529. }
  530. mpc8xxx_spi = spi_master_get_devdata(spi->master);
  531. hw_mode = cs->hw_mode; /* Save orginal settings */
  532. cs->hw_mode = mpc8xxx_spi_read_reg(&mpc8xxx_spi->base->mode);
  533. /* mask out bits we are going to set */
  534. cs->hw_mode &= ~(SPMODE_CP_BEGIN_EDGECLK | SPMODE_CI_INACTIVEHIGH
  535. | SPMODE_REV | SPMODE_LOOP);
  536. if (spi->mode & SPI_CPHA)
  537. cs->hw_mode |= SPMODE_CP_BEGIN_EDGECLK;
  538. if (spi->mode & SPI_CPOL)
  539. cs->hw_mode |= SPMODE_CI_INACTIVEHIGH;
  540. if (!(spi->mode & SPI_LSB_FIRST))
  541. cs->hw_mode |= SPMODE_REV;
  542. if (spi->mode & SPI_LOOP)
  543. cs->hw_mode |= SPMODE_LOOP;
  544. retval = mpc8xxx_spi_setup_transfer(spi, NULL);
  545. if (retval < 0) {
  546. cs->hw_mode = hw_mode; /* Restore settings */
  547. return retval;
  548. }
  549. return 0;
  550. }
  551. static void mpc8xxx_spi_cpm_irq(struct mpc8xxx_spi *mspi, u32 events)
  552. {
  553. u16 len;
  554. dev_dbg(mspi->dev, "%s: bd datlen %d, count %d\n", __func__,
  555. in_be16(&mspi->rx_bd->cbd_datlen), mspi->count);
  556. len = in_be16(&mspi->rx_bd->cbd_datlen);
  557. if (len > mspi->count) {
  558. WARN_ON(1);
  559. len = mspi->count;
  560. }
  561. /* Clear the events */
  562. mpc8xxx_spi_write_reg(&mspi->base->event, events);
  563. mspi->count -= len;
  564. if (mspi->count)
  565. mpc8xxx_spi_cpm_bufs_start(mspi);
  566. else
  567. complete(&mspi->done);
  568. }
  569. static void mpc8xxx_spi_cpu_irq(struct mpc8xxx_spi *mspi, u32 events)
  570. {
  571. /* We need handle RX first */
  572. if (events & SPIE_NE) {
  573. u32 rx_data = mpc8xxx_spi_read_reg(&mspi->base->receive);
  574. if (mspi->rx)
  575. mspi->get_rx(rx_data, mspi);
  576. }
  577. if ((events & SPIE_NF) == 0)
  578. /* spin until TX is done */
  579. while (((events =
  580. mpc8xxx_spi_read_reg(&mspi->base->event)) &
  581. SPIE_NF) == 0)
  582. cpu_relax();
  583. /* Clear the events */
  584. mpc8xxx_spi_write_reg(&mspi->base->event, events);
  585. mspi->count -= 1;
  586. if (mspi->count) {
  587. u32 word = mspi->get_tx(mspi);
  588. mpc8xxx_spi_write_reg(&mspi->base->transmit, word);
  589. } else {
  590. complete(&mspi->done);
  591. }
  592. }
  593. static irqreturn_t mpc8xxx_spi_irq(s32 irq, void *context_data)
  594. {
  595. struct mpc8xxx_spi *mspi = context_data;
  596. irqreturn_t ret = IRQ_NONE;
  597. u32 events;
  598. /* Get interrupt events(tx/rx) */
  599. events = mpc8xxx_spi_read_reg(&mspi->base->event);
  600. if (events)
  601. ret = IRQ_HANDLED;
  602. dev_dbg(mspi->dev, "%s: events %x\n", __func__, events);
  603. if (mspi->flags & SPI_CPM_MODE)
  604. mpc8xxx_spi_cpm_irq(mspi, events);
  605. else
  606. mpc8xxx_spi_cpu_irq(mspi, events);
  607. return ret;
  608. }
  609. static int mpc8xxx_spi_transfer(struct spi_device *spi,
  610. struct spi_message *m)
  611. {
  612. struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(spi->master);
  613. unsigned long flags;
  614. m->actual_length = 0;
  615. m->status = -EINPROGRESS;
  616. spin_lock_irqsave(&mpc8xxx_spi->lock, flags);
  617. list_add_tail(&m->queue, &mpc8xxx_spi->queue);
  618. queue_work(mpc8xxx_spi->workqueue, &mpc8xxx_spi->work);
  619. spin_unlock_irqrestore(&mpc8xxx_spi->lock, flags);
  620. return 0;
  621. }
  622. static void mpc8xxx_spi_cleanup(struct spi_device *spi)
  623. {
  624. kfree(spi->controller_state);
  625. }
  626. static void *mpc8xxx_spi_alloc_dummy_rx(void)
  627. {
  628. mutex_lock(&mpc8xxx_dummy_rx_lock);
  629. if (!mpc8xxx_dummy_rx)
  630. mpc8xxx_dummy_rx = kmalloc(SPI_MRBLR, GFP_KERNEL);
  631. if (mpc8xxx_dummy_rx)
  632. mpc8xxx_dummy_rx_refcnt++;
  633. mutex_unlock(&mpc8xxx_dummy_rx_lock);
  634. return mpc8xxx_dummy_rx;
  635. }
  636. static void mpc8xxx_spi_free_dummy_rx(void)
  637. {
  638. mutex_lock(&mpc8xxx_dummy_rx_lock);
  639. switch (mpc8xxx_dummy_rx_refcnt) {
  640. case 0:
  641. WARN_ON(1);
  642. break;
  643. case 1:
  644. kfree(mpc8xxx_dummy_rx);
  645. mpc8xxx_dummy_rx = NULL;
  646. /* fall through */
  647. default:
  648. mpc8xxx_dummy_rx_refcnt--;
  649. break;
  650. }
  651. mutex_unlock(&mpc8xxx_dummy_rx_lock);
  652. }
  653. static unsigned long mpc8xxx_spi_cpm_get_pram(struct mpc8xxx_spi *mspi)
  654. {
  655. struct device *dev = mspi->dev;
  656. struct device_node *np = dev_archdata_get_node(&dev->archdata);
  657. const u32 *iprop;
  658. int size;
  659. unsigned long spi_base_ofs;
  660. unsigned long pram_ofs = -ENOMEM;
  661. /* Can't use of_address_to_resource(), QE muram isn't at 0. */
  662. iprop = of_get_property(np, "reg", &size);
  663. /* QE with a fixed pram location? */
  664. if (mspi->flags & SPI_QE && iprop && size == sizeof(*iprop) * 4)
  665. return cpm_muram_alloc_fixed(iprop[2], SPI_PRAM_SIZE);
  666. /* QE but with a dynamic pram location? */
  667. if (mspi->flags & SPI_QE) {
  668. pram_ofs = cpm_muram_alloc(SPI_PRAM_SIZE, 64);
  669. qe_issue_cmd(QE_ASSIGN_PAGE_TO_DEVICE, mspi->subblock,
  670. QE_CR_PROTOCOL_UNSPECIFIED, pram_ofs);
  671. return pram_ofs;
  672. }
  673. /* CPM1 and CPM2 pram must be at a fixed addr. */
  674. if (!iprop || size != sizeof(*iprop) * 4)
  675. return -ENOMEM;
  676. spi_base_ofs = cpm_muram_alloc_fixed(iprop[2], 2);
  677. if (IS_ERR_VALUE(spi_base_ofs))
  678. return -ENOMEM;
  679. if (mspi->flags & SPI_CPM2) {
  680. pram_ofs = cpm_muram_alloc(SPI_PRAM_SIZE, 64);
  681. if (!IS_ERR_VALUE(pram_ofs)) {
  682. u16 __iomem *spi_base = cpm_muram_addr(spi_base_ofs);
  683. out_be16(spi_base, pram_ofs);
  684. }
  685. } else {
  686. struct spi_pram __iomem *pram = cpm_muram_addr(spi_base_ofs);
  687. u16 rpbase = in_be16(&pram->rpbase);
  688. /* Microcode relocation patch applied? */
  689. if (rpbase)
  690. pram_ofs = rpbase;
  691. else
  692. return spi_base_ofs;
  693. }
  694. cpm_muram_free(spi_base_ofs);
  695. return pram_ofs;
  696. }
  697. static int mpc8xxx_spi_cpm_init(struct mpc8xxx_spi *mspi)
  698. {
  699. struct device *dev = mspi->dev;
  700. struct device_node *np = dev_archdata_get_node(&dev->archdata);
  701. const u32 *iprop;
  702. int size;
  703. unsigned long pram_ofs;
  704. unsigned long bds_ofs;
  705. if (!(mspi->flags & SPI_CPM_MODE))
  706. return 0;
  707. if (!mpc8xxx_spi_alloc_dummy_rx())
  708. return -ENOMEM;
  709. if (mspi->flags & SPI_QE) {
  710. iprop = of_get_property(np, "cell-index", &size);
  711. if (iprop && size == sizeof(*iprop))
  712. mspi->subblock = *iprop;
  713. switch (mspi->subblock) {
  714. default:
  715. dev_warn(dev, "cell-index unspecified, assuming SPI1");
  716. /* fall through */
  717. case 0:
  718. mspi->subblock = QE_CR_SUBBLOCK_SPI1;
  719. break;
  720. case 1:
  721. mspi->subblock = QE_CR_SUBBLOCK_SPI2;
  722. break;
  723. }
  724. }
  725. pram_ofs = mpc8xxx_spi_cpm_get_pram(mspi);
  726. if (IS_ERR_VALUE(pram_ofs)) {
  727. dev_err(dev, "can't allocate spi parameter ram\n");
  728. goto err_pram;
  729. }
  730. bds_ofs = cpm_muram_alloc(sizeof(*mspi->tx_bd) +
  731. sizeof(*mspi->rx_bd), 8);
  732. if (IS_ERR_VALUE(bds_ofs)) {
  733. dev_err(dev, "can't allocate bds\n");
  734. goto err_bds;
  735. }
  736. mspi->dma_dummy_tx = dma_map_single(dev, empty_zero_page, PAGE_SIZE,
  737. DMA_TO_DEVICE);
  738. if (dma_mapping_error(dev, mspi->dma_dummy_tx)) {
  739. dev_err(dev, "unable to map dummy tx buffer\n");
  740. goto err_dummy_tx;
  741. }
  742. mspi->dma_dummy_rx = dma_map_single(dev, mpc8xxx_dummy_rx, SPI_MRBLR,
  743. DMA_FROM_DEVICE);
  744. if (dma_mapping_error(dev, mspi->dma_dummy_rx)) {
  745. dev_err(dev, "unable to map dummy rx buffer\n");
  746. goto err_dummy_rx;
  747. }
  748. mspi->pram = cpm_muram_addr(pram_ofs);
  749. mspi->tx_bd = cpm_muram_addr(bds_ofs);
  750. mspi->rx_bd = cpm_muram_addr(bds_ofs + sizeof(*mspi->tx_bd));
  751. /* Initialize parameter ram. */
  752. out_be16(&mspi->pram->tbase, cpm_muram_offset(mspi->tx_bd));
  753. out_be16(&mspi->pram->rbase, cpm_muram_offset(mspi->rx_bd));
  754. out_8(&mspi->pram->tfcr, CPMFCR_EB | CPMFCR_GBL);
  755. out_8(&mspi->pram->rfcr, CPMFCR_EB | CPMFCR_GBL);
  756. out_be16(&mspi->pram->mrblr, SPI_MRBLR);
  757. out_be32(&mspi->pram->rstate, 0);
  758. out_be32(&mspi->pram->rdp, 0);
  759. out_be16(&mspi->pram->rbptr, 0);
  760. out_be16(&mspi->pram->rbc, 0);
  761. out_be32(&mspi->pram->rxtmp, 0);
  762. out_be32(&mspi->pram->tstate, 0);
  763. out_be32(&mspi->pram->tdp, 0);
  764. out_be16(&mspi->pram->tbptr, 0);
  765. out_be16(&mspi->pram->tbc, 0);
  766. out_be32(&mspi->pram->txtmp, 0);
  767. return 0;
  768. err_dummy_rx:
  769. dma_unmap_single(dev, mspi->dma_dummy_tx, PAGE_SIZE, DMA_TO_DEVICE);
  770. err_dummy_tx:
  771. cpm_muram_free(bds_ofs);
  772. err_bds:
  773. cpm_muram_free(pram_ofs);
  774. err_pram:
  775. mpc8xxx_spi_free_dummy_rx();
  776. return -ENOMEM;
  777. }
  778. static void mpc8xxx_spi_cpm_free(struct mpc8xxx_spi *mspi)
  779. {
  780. struct device *dev = mspi->dev;
  781. dma_unmap_single(dev, mspi->dma_dummy_rx, SPI_MRBLR, DMA_FROM_DEVICE);
  782. dma_unmap_single(dev, mspi->dma_dummy_tx, PAGE_SIZE, DMA_TO_DEVICE);
  783. cpm_muram_free(cpm_muram_offset(mspi->tx_bd));
  784. cpm_muram_free(cpm_muram_offset(mspi->pram));
  785. mpc8xxx_spi_free_dummy_rx();
  786. }
  787. static const char *mpc8xxx_spi_strmode(unsigned int flags)
  788. {
  789. if (flags & SPI_QE_CPU_MODE) {
  790. return "QE CPU";
  791. } else if (flags & SPI_CPM_MODE) {
  792. if (flags & SPI_QE)
  793. return "QE";
  794. else if (flags & SPI_CPM2)
  795. return "CPM2";
  796. else
  797. return "CPM1";
  798. }
  799. return "CPU";
  800. }
  801. static struct spi_master * __devinit
  802. mpc8xxx_spi_probe(struct device *dev, struct resource *mem, unsigned int irq)
  803. {
  804. struct fsl_spi_platform_data *pdata = dev->platform_data;
  805. struct spi_master *master;
  806. struct mpc8xxx_spi *mpc8xxx_spi;
  807. u32 regval;
  808. int ret = 0;
  809. master = spi_alloc_master(dev, sizeof(struct mpc8xxx_spi));
  810. if (master == NULL) {
  811. ret = -ENOMEM;
  812. goto err;
  813. }
  814. dev_set_drvdata(dev, master);
  815. /* the spi->mode bits understood by this driver: */
  816. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH
  817. | SPI_LSB_FIRST | SPI_LOOP;
  818. master->setup = mpc8xxx_spi_setup;
  819. master->transfer = mpc8xxx_spi_transfer;
  820. master->cleanup = mpc8xxx_spi_cleanup;
  821. mpc8xxx_spi = spi_master_get_devdata(master);
  822. mpc8xxx_spi->dev = dev;
  823. mpc8xxx_spi->get_rx = mpc8xxx_spi_rx_buf_u8;
  824. mpc8xxx_spi->get_tx = mpc8xxx_spi_tx_buf_u8;
  825. mpc8xxx_spi->flags = pdata->flags;
  826. mpc8xxx_spi->spibrg = pdata->sysclk;
  827. ret = mpc8xxx_spi_cpm_init(mpc8xxx_spi);
  828. if (ret)
  829. goto err_cpm_init;
  830. mpc8xxx_spi->rx_shift = 0;
  831. mpc8xxx_spi->tx_shift = 0;
  832. if (mpc8xxx_spi->flags & SPI_QE_CPU_MODE) {
  833. mpc8xxx_spi->rx_shift = 16;
  834. mpc8xxx_spi->tx_shift = 24;
  835. }
  836. init_completion(&mpc8xxx_spi->done);
  837. mpc8xxx_spi->base = ioremap(mem->start, resource_size(mem));
  838. if (mpc8xxx_spi->base == NULL) {
  839. ret = -ENOMEM;
  840. goto err_ioremap;
  841. }
  842. mpc8xxx_spi->irq = irq;
  843. /* Register for SPI Interrupt */
  844. ret = request_irq(mpc8xxx_spi->irq, mpc8xxx_spi_irq,
  845. 0, "mpc8xxx_spi", mpc8xxx_spi);
  846. if (ret != 0)
  847. goto unmap_io;
  848. master->bus_num = pdata->bus_num;
  849. master->num_chipselect = pdata->max_chipselect;
  850. /* SPI controller initializations */
  851. mpc8xxx_spi_write_reg(&mpc8xxx_spi->base->mode, 0);
  852. mpc8xxx_spi_write_reg(&mpc8xxx_spi->base->mask, 0);
  853. mpc8xxx_spi_write_reg(&mpc8xxx_spi->base->command, 0);
  854. mpc8xxx_spi_write_reg(&mpc8xxx_spi->base->event, 0xffffffff);
  855. /* Enable SPI interface */
  856. regval = pdata->initial_spmode | SPMODE_INIT_VAL | SPMODE_ENABLE;
  857. if (mpc8xxx_spi->flags & SPI_QE_CPU_MODE)
  858. regval |= SPMODE_OP;
  859. mpc8xxx_spi_write_reg(&mpc8xxx_spi->base->mode, regval);
  860. spin_lock_init(&mpc8xxx_spi->lock);
  861. init_completion(&mpc8xxx_spi->done);
  862. INIT_WORK(&mpc8xxx_spi->work, mpc8xxx_spi_work);
  863. INIT_LIST_HEAD(&mpc8xxx_spi->queue);
  864. mpc8xxx_spi->workqueue = create_singlethread_workqueue(
  865. dev_name(master->dev.parent));
  866. if (mpc8xxx_spi->workqueue == NULL) {
  867. ret = -EBUSY;
  868. goto free_irq;
  869. }
  870. ret = spi_register_master(master);
  871. if (ret < 0)
  872. goto unreg_master;
  873. dev_info(dev, "at 0x%p (irq = %d), %s mode\n", mpc8xxx_spi->base,
  874. mpc8xxx_spi->irq, mpc8xxx_spi_strmode(mpc8xxx_spi->flags));
  875. return master;
  876. unreg_master:
  877. destroy_workqueue(mpc8xxx_spi->workqueue);
  878. free_irq:
  879. free_irq(mpc8xxx_spi->irq, mpc8xxx_spi);
  880. unmap_io:
  881. iounmap(mpc8xxx_spi->base);
  882. err_ioremap:
  883. mpc8xxx_spi_cpm_free(mpc8xxx_spi);
  884. err_cpm_init:
  885. spi_master_put(master);
  886. err:
  887. return ERR_PTR(ret);
  888. }
  889. static int __devexit mpc8xxx_spi_remove(struct device *dev)
  890. {
  891. struct mpc8xxx_spi *mpc8xxx_spi;
  892. struct spi_master *master;
  893. master = dev_get_drvdata(dev);
  894. mpc8xxx_spi = spi_master_get_devdata(master);
  895. flush_workqueue(mpc8xxx_spi->workqueue);
  896. destroy_workqueue(mpc8xxx_spi->workqueue);
  897. spi_unregister_master(master);
  898. free_irq(mpc8xxx_spi->irq, mpc8xxx_spi);
  899. iounmap(mpc8xxx_spi->base);
  900. mpc8xxx_spi_cpm_free(mpc8xxx_spi);
  901. return 0;
  902. }
  903. struct mpc8xxx_spi_probe_info {
  904. struct fsl_spi_platform_data pdata;
  905. int *gpios;
  906. bool *alow_flags;
  907. };
  908. static struct mpc8xxx_spi_probe_info *
  909. to_of_pinfo(struct fsl_spi_platform_data *pdata)
  910. {
  911. return container_of(pdata, struct mpc8xxx_spi_probe_info, pdata);
  912. }
  913. static void mpc8xxx_spi_cs_control(struct spi_device *spi, bool on)
  914. {
  915. struct device *dev = spi->dev.parent;
  916. struct mpc8xxx_spi_probe_info *pinfo = to_of_pinfo(dev->platform_data);
  917. u16 cs = spi->chip_select;
  918. int gpio = pinfo->gpios[cs];
  919. bool alow = pinfo->alow_flags[cs];
  920. gpio_set_value(gpio, on ^ alow);
  921. }
  922. static int of_mpc8xxx_spi_get_chipselects(struct device *dev)
  923. {
  924. struct device_node *np = dev_archdata_get_node(&dev->archdata);
  925. struct fsl_spi_platform_data *pdata = dev->platform_data;
  926. struct mpc8xxx_spi_probe_info *pinfo = to_of_pinfo(pdata);
  927. unsigned int ngpios;
  928. int i = 0;
  929. int ret;
  930. ngpios = of_gpio_count(np);
  931. if (!ngpios) {
  932. /*
  933. * SPI w/o chip-select line. One SPI device is still permitted
  934. * though.
  935. */
  936. pdata->max_chipselect = 1;
  937. return 0;
  938. }
  939. pinfo->gpios = kmalloc(ngpios * sizeof(*pinfo->gpios), GFP_KERNEL);
  940. if (!pinfo->gpios)
  941. return -ENOMEM;
  942. memset(pinfo->gpios, -1, ngpios * sizeof(*pinfo->gpios));
  943. pinfo->alow_flags = kzalloc(ngpios * sizeof(*pinfo->alow_flags),
  944. GFP_KERNEL);
  945. if (!pinfo->alow_flags) {
  946. ret = -ENOMEM;
  947. goto err_alloc_flags;
  948. }
  949. for (; i < ngpios; i++) {
  950. int gpio;
  951. enum of_gpio_flags flags;
  952. gpio = of_get_gpio_flags(np, i, &flags);
  953. if (!gpio_is_valid(gpio)) {
  954. dev_err(dev, "invalid gpio #%d: %d\n", i, gpio);
  955. ret = gpio;
  956. goto err_loop;
  957. }
  958. ret = gpio_request(gpio, dev_name(dev));
  959. if (ret) {
  960. dev_err(dev, "can't request gpio #%d: %d\n", i, ret);
  961. goto err_loop;
  962. }
  963. pinfo->gpios[i] = gpio;
  964. pinfo->alow_flags[i] = flags & OF_GPIO_ACTIVE_LOW;
  965. ret = gpio_direction_output(pinfo->gpios[i],
  966. pinfo->alow_flags[i]);
  967. if (ret) {
  968. dev_err(dev, "can't set output direction for gpio "
  969. "#%d: %d\n", i, ret);
  970. goto err_loop;
  971. }
  972. }
  973. pdata->max_chipselect = ngpios;
  974. pdata->cs_control = mpc8xxx_spi_cs_control;
  975. return 0;
  976. err_loop:
  977. while (i >= 0) {
  978. if (gpio_is_valid(pinfo->gpios[i]))
  979. gpio_free(pinfo->gpios[i]);
  980. i--;
  981. }
  982. kfree(pinfo->alow_flags);
  983. pinfo->alow_flags = NULL;
  984. err_alloc_flags:
  985. kfree(pinfo->gpios);
  986. pinfo->gpios = NULL;
  987. return ret;
  988. }
  989. static int of_mpc8xxx_spi_free_chipselects(struct device *dev)
  990. {
  991. struct fsl_spi_platform_data *pdata = dev->platform_data;
  992. struct mpc8xxx_spi_probe_info *pinfo = to_of_pinfo(pdata);
  993. int i;
  994. if (!pinfo->gpios)
  995. return 0;
  996. for (i = 0; i < pdata->max_chipselect; i++) {
  997. if (gpio_is_valid(pinfo->gpios[i]))
  998. gpio_free(pinfo->gpios[i]);
  999. }
  1000. kfree(pinfo->gpios);
  1001. kfree(pinfo->alow_flags);
  1002. return 0;
  1003. }
  1004. static int __devinit of_mpc8xxx_spi_probe(struct of_device *ofdev,
  1005. const struct of_device_id *ofid)
  1006. {
  1007. struct device *dev = &ofdev->dev;
  1008. struct device_node *np = ofdev->node;
  1009. struct mpc8xxx_spi_probe_info *pinfo;
  1010. struct fsl_spi_platform_data *pdata;
  1011. struct spi_master *master;
  1012. struct resource mem;
  1013. struct resource irq;
  1014. const void *prop;
  1015. int ret = -ENOMEM;
  1016. pinfo = kzalloc(sizeof(*pinfo), GFP_KERNEL);
  1017. if (!pinfo)
  1018. return -ENOMEM;
  1019. pdata = &pinfo->pdata;
  1020. dev->platform_data = pdata;
  1021. /* Allocate bus num dynamically. */
  1022. pdata->bus_num = -1;
  1023. /* SPI controller is either clocked from QE or SoC clock. */
  1024. pdata->sysclk = get_brgfreq();
  1025. if (pdata->sysclk == -1) {
  1026. pdata->sysclk = fsl_get_sys_freq();
  1027. if (pdata->sysclk == -1) {
  1028. ret = -ENODEV;
  1029. goto err_clk;
  1030. }
  1031. }
  1032. prop = of_get_property(np, "mode", NULL);
  1033. if (prop && !strcmp(prop, "cpu-qe"))
  1034. pdata->flags = SPI_QE_CPU_MODE;
  1035. else if (prop && !strcmp(prop, "qe"))
  1036. pdata->flags = SPI_CPM_MODE | SPI_QE;
  1037. else if (of_device_is_compatible(np, "fsl,cpm2-spi"))
  1038. pdata->flags = SPI_CPM_MODE | SPI_CPM2;
  1039. else if (of_device_is_compatible(np, "fsl,cpm1-spi"))
  1040. pdata->flags = SPI_CPM_MODE | SPI_CPM1;
  1041. ret = of_mpc8xxx_spi_get_chipselects(dev);
  1042. if (ret)
  1043. goto err;
  1044. ret = of_address_to_resource(np, 0, &mem);
  1045. if (ret)
  1046. goto err;
  1047. ret = of_irq_to_resource(np, 0, &irq);
  1048. if (!ret) {
  1049. ret = -EINVAL;
  1050. goto err;
  1051. }
  1052. master = mpc8xxx_spi_probe(dev, &mem, irq.start);
  1053. if (IS_ERR(master)) {
  1054. ret = PTR_ERR(master);
  1055. goto err;
  1056. }
  1057. of_register_spi_devices(master, np);
  1058. return 0;
  1059. err:
  1060. of_mpc8xxx_spi_free_chipselects(dev);
  1061. err_clk:
  1062. kfree(pinfo);
  1063. return ret;
  1064. }
  1065. static int __devexit of_mpc8xxx_spi_remove(struct of_device *ofdev)
  1066. {
  1067. int ret;
  1068. ret = mpc8xxx_spi_remove(&ofdev->dev);
  1069. if (ret)
  1070. return ret;
  1071. of_mpc8xxx_spi_free_chipselects(&ofdev->dev);
  1072. return 0;
  1073. }
  1074. static const struct of_device_id of_mpc8xxx_spi_match[] = {
  1075. { .compatible = "fsl,spi" },
  1076. {},
  1077. };
  1078. MODULE_DEVICE_TABLE(of, of_mpc8xxx_spi_match);
  1079. static struct of_platform_driver of_mpc8xxx_spi_driver = {
  1080. .name = "mpc8xxx_spi",
  1081. .match_table = of_mpc8xxx_spi_match,
  1082. .probe = of_mpc8xxx_spi_probe,
  1083. .remove = __devexit_p(of_mpc8xxx_spi_remove),
  1084. };
  1085. #ifdef CONFIG_MPC832x_RDB
  1086. /*
  1087. * XXX XXX XXX
  1088. * This is "legacy" platform driver, was used by the MPC8323E-RDB boards
  1089. * only. The driver should go away soon, since newer MPC8323E-RDB's device
  1090. * tree can work with OpenFirmware driver. But for now we support old trees
  1091. * as well.
  1092. */
  1093. static int __devinit plat_mpc8xxx_spi_probe(struct platform_device *pdev)
  1094. {
  1095. struct resource *mem;
  1096. unsigned int irq;
  1097. struct spi_master *master;
  1098. if (!pdev->dev.platform_data)
  1099. return -EINVAL;
  1100. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1101. if (!mem)
  1102. return -EINVAL;
  1103. irq = platform_get_irq(pdev, 0);
  1104. if (!irq)
  1105. return -EINVAL;
  1106. master = mpc8xxx_spi_probe(&pdev->dev, mem, irq);
  1107. if (IS_ERR(master))
  1108. return PTR_ERR(master);
  1109. return 0;
  1110. }
  1111. static int __devexit plat_mpc8xxx_spi_remove(struct platform_device *pdev)
  1112. {
  1113. return mpc8xxx_spi_remove(&pdev->dev);
  1114. }
  1115. MODULE_ALIAS("platform:mpc8xxx_spi");
  1116. static struct platform_driver mpc8xxx_spi_driver = {
  1117. .probe = plat_mpc8xxx_spi_probe,
  1118. .remove = __devexit_p(plat_mpc8xxx_spi_remove),
  1119. .driver = {
  1120. .name = "mpc8xxx_spi",
  1121. .owner = THIS_MODULE,
  1122. },
  1123. };
  1124. static bool legacy_driver_failed;
  1125. static void __init legacy_driver_register(void)
  1126. {
  1127. legacy_driver_failed = platform_driver_register(&mpc8xxx_spi_driver);
  1128. }
  1129. static void __exit legacy_driver_unregister(void)
  1130. {
  1131. if (legacy_driver_failed)
  1132. return;
  1133. platform_driver_unregister(&mpc8xxx_spi_driver);
  1134. }
  1135. #else
  1136. static void __init legacy_driver_register(void) {}
  1137. static void __exit legacy_driver_unregister(void) {}
  1138. #endif /* CONFIG_MPC832x_RDB */
  1139. static int __init mpc8xxx_spi_init(void)
  1140. {
  1141. legacy_driver_register();
  1142. return of_register_platform_driver(&of_mpc8xxx_spi_driver);
  1143. }
  1144. static void __exit mpc8xxx_spi_exit(void)
  1145. {
  1146. of_unregister_platform_driver(&of_mpc8xxx_spi_driver);
  1147. legacy_driver_unregister();
  1148. }
  1149. module_init(mpc8xxx_spi_init);
  1150. module_exit(mpc8xxx_spi_exit);
  1151. MODULE_AUTHOR("Kumar Gala");
  1152. MODULE_DESCRIPTION("Simple MPC8xxx SPI Driver");
  1153. MODULE_LICENSE("GPL");