spi-pxa2xx.c 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918
  1. /*
  2. * Copyright (C) 2005 Stephen Street / StreetFire Sound Labs
  3. * Copyright (C) 2013, Intel Corporation
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include <linux/bitops.h>
  16. #include <linux/init.h>
  17. #include <linux/module.h>
  18. #include <linux/device.h>
  19. #include <linux/ioport.h>
  20. #include <linux/errno.h>
  21. #include <linux/err.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/kernel.h>
  24. #include <linux/pci.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/spi/pxa2xx_spi.h>
  27. #include <linux/spi/spi.h>
  28. #include <linux/delay.h>
  29. #include <linux/gpio.h>
  30. #include <linux/gpio/consumer.h>
  31. #include <linux/slab.h>
  32. #include <linux/clk.h>
  33. #include <linux/pm_runtime.h>
  34. #include <linux/acpi.h>
  35. #include "spi-pxa2xx.h"
  36. MODULE_AUTHOR("Stephen Street");
  37. MODULE_DESCRIPTION("PXA2xx SSP SPI Controller");
  38. MODULE_LICENSE("GPL");
  39. MODULE_ALIAS("platform:pxa2xx-spi");
  40. #define TIMOUT_DFLT 1000
  41. /*
  42. * for testing SSCR1 changes that require SSP restart, basically
  43. * everything except the service and interrupt enables, the pxa270 developer
  44. * manual says only SSCR1_SCFR, SSCR1_SPH, SSCR1_SPO need to be in this
  45. * list, but the PXA255 dev man says all bits without really meaning the
  46. * service and interrupt enables
  47. */
  48. #define SSCR1_CHANGE_MASK (SSCR1_TTELP | SSCR1_TTE | SSCR1_SCFR \
  49. | SSCR1_ECRA | SSCR1_ECRB | SSCR1_SCLKDIR \
  50. | SSCR1_SFRMDIR | SSCR1_RWOT | SSCR1_TRAIL \
  51. | SSCR1_IFS | SSCR1_STRF | SSCR1_EFWR \
  52. | SSCR1_RFT | SSCR1_TFT | SSCR1_MWDS \
  53. | SSCR1_SPH | SSCR1_SPO | SSCR1_LBM)
  54. #define QUARK_X1000_SSCR1_CHANGE_MASK (QUARK_X1000_SSCR1_STRF \
  55. | QUARK_X1000_SSCR1_EFWR \
  56. | QUARK_X1000_SSCR1_RFT \
  57. | QUARK_X1000_SSCR1_TFT \
  58. | SSCR1_SPH | SSCR1_SPO | SSCR1_LBM)
  59. #define CE4100_SSCR1_CHANGE_MASK (SSCR1_TTELP | SSCR1_TTE | SSCR1_SCFR \
  60. | SSCR1_ECRA | SSCR1_ECRB | SSCR1_SCLKDIR \
  61. | SSCR1_SFRMDIR | SSCR1_RWOT | SSCR1_TRAIL \
  62. | SSCR1_IFS | SSCR1_STRF | SSCR1_EFWR \
  63. | CE4100_SSCR1_RFT | CE4100_SSCR1_TFT | SSCR1_MWDS \
  64. | SSCR1_SPH | SSCR1_SPO | SSCR1_LBM)
  65. #define LPSS_GENERAL_REG_RXTO_HOLDOFF_DISABLE BIT(24)
  66. #define LPSS_CS_CONTROL_SW_MODE BIT(0)
  67. #define LPSS_CS_CONTROL_CS_HIGH BIT(1)
  68. #define LPSS_CAPS_CS_EN_SHIFT 9
  69. #define LPSS_CAPS_CS_EN_MASK (0xf << LPSS_CAPS_CS_EN_SHIFT)
  70. struct lpss_config {
  71. /* LPSS offset from drv_data->ioaddr */
  72. unsigned offset;
  73. /* Register offsets from drv_data->lpss_base or -1 */
  74. int reg_general;
  75. int reg_ssp;
  76. int reg_cs_ctrl;
  77. int reg_capabilities;
  78. /* FIFO thresholds */
  79. u32 rx_threshold;
  80. u32 tx_threshold_lo;
  81. u32 tx_threshold_hi;
  82. /* Chip select control */
  83. unsigned cs_sel_shift;
  84. unsigned cs_sel_mask;
  85. unsigned cs_num;
  86. };
  87. /* Keep these sorted with enum pxa_ssp_type */
  88. static const struct lpss_config lpss_platforms[] = {
  89. { /* LPSS_LPT_SSP */
  90. .offset = 0x800,
  91. .reg_general = 0x08,
  92. .reg_ssp = 0x0c,
  93. .reg_cs_ctrl = 0x18,
  94. .reg_capabilities = -1,
  95. .rx_threshold = 64,
  96. .tx_threshold_lo = 160,
  97. .tx_threshold_hi = 224,
  98. },
  99. { /* LPSS_BYT_SSP */
  100. .offset = 0x400,
  101. .reg_general = 0x08,
  102. .reg_ssp = 0x0c,
  103. .reg_cs_ctrl = 0x18,
  104. .reg_capabilities = -1,
  105. .rx_threshold = 64,
  106. .tx_threshold_lo = 160,
  107. .tx_threshold_hi = 224,
  108. },
  109. { /* LPSS_BSW_SSP */
  110. .offset = 0x400,
  111. .reg_general = 0x08,
  112. .reg_ssp = 0x0c,
  113. .reg_cs_ctrl = 0x18,
  114. .reg_capabilities = -1,
  115. .rx_threshold = 64,
  116. .tx_threshold_lo = 160,
  117. .tx_threshold_hi = 224,
  118. .cs_sel_shift = 2,
  119. .cs_sel_mask = 1 << 2,
  120. .cs_num = 2,
  121. },
  122. { /* LPSS_SPT_SSP */
  123. .offset = 0x200,
  124. .reg_general = -1,
  125. .reg_ssp = 0x20,
  126. .reg_cs_ctrl = 0x24,
  127. .reg_capabilities = -1,
  128. .rx_threshold = 1,
  129. .tx_threshold_lo = 32,
  130. .tx_threshold_hi = 56,
  131. },
  132. { /* LPSS_BXT_SSP */
  133. .offset = 0x200,
  134. .reg_general = -1,
  135. .reg_ssp = 0x20,
  136. .reg_cs_ctrl = 0x24,
  137. .reg_capabilities = 0xfc,
  138. .rx_threshold = 1,
  139. .tx_threshold_lo = 16,
  140. .tx_threshold_hi = 48,
  141. .cs_sel_shift = 8,
  142. .cs_sel_mask = 3 << 8,
  143. },
  144. };
  145. static inline const struct lpss_config
  146. *lpss_get_config(const struct driver_data *drv_data)
  147. {
  148. return &lpss_platforms[drv_data->ssp_type - LPSS_LPT_SSP];
  149. }
  150. static bool is_lpss_ssp(const struct driver_data *drv_data)
  151. {
  152. switch (drv_data->ssp_type) {
  153. case LPSS_LPT_SSP:
  154. case LPSS_BYT_SSP:
  155. case LPSS_BSW_SSP:
  156. case LPSS_SPT_SSP:
  157. case LPSS_BXT_SSP:
  158. return true;
  159. default:
  160. return false;
  161. }
  162. }
  163. static bool is_quark_x1000_ssp(const struct driver_data *drv_data)
  164. {
  165. return drv_data->ssp_type == QUARK_X1000_SSP;
  166. }
  167. static u32 pxa2xx_spi_get_ssrc1_change_mask(const struct driver_data *drv_data)
  168. {
  169. switch (drv_data->ssp_type) {
  170. case QUARK_X1000_SSP:
  171. return QUARK_X1000_SSCR1_CHANGE_MASK;
  172. case CE4100_SSP:
  173. return CE4100_SSCR1_CHANGE_MASK;
  174. default:
  175. return SSCR1_CHANGE_MASK;
  176. }
  177. }
  178. static u32
  179. pxa2xx_spi_get_rx_default_thre(const struct driver_data *drv_data)
  180. {
  181. switch (drv_data->ssp_type) {
  182. case QUARK_X1000_SSP:
  183. return RX_THRESH_QUARK_X1000_DFLT;
  184. case CE4100_SSP:
  185. return RX_THRESH_CE4100_DFLT;
  186. default:
  187. return RX_THRESH_DFLT;
  188. }
  189. }
  190. static bool pxa2xx_spi_txfifo_full(const struct driver_data *drv_data)
  191. {
  192. u32 mask;
  193. switch (drv_data->ssp_type) {
  194. case QUARK_X1000_SSP:
  195. mask = QUARK_X1000_SSSR_TFL_MASK;
  196. break;
  197. case CE4100_SSP:
  198. mask = CE4100_SSSR_TFL_MASK;
  199. break;
  200. default:
  201. mask = SSSR_TFL_MASK;
  202. break;
  203. }
  204. return (pxa2xx_spi_read(drv_data, SSSR) & mask) == mask;
  205. }
  206. static void pxa2xx_spi_clear_rx_thre(const struct driver_data *drv_data,
  207. u32 *sccr1_reg)
  208. {
  209. u32 mask;
  210. switch (drv_data->ssp_type) {
  211. case QUARK_X1000_SSP:
  212. mask = QUARK_X1000_SSCR1_RFT;
  213. break;
  214. case CE4100_SSP:
  215. mask = CE4100_SSCR1_RFT;
  216. break;
  217. default:
  218. mask = SSCR1_RFT;
  219. break;
  220. }
  221. *sccr1_reg &= ~mask;
  222. }
  223. static void pxa2xx_spi_set_rx_thre(const struct driver_data *drv_data,
  224. u32 *sccr1_reg, u32 threshold)
  225. {
  226. switch (drv_data->ssp_type) {
  227. case QUARK_X1000_SSP:
  228. *sccr1_reg |= QUARK_X1000_SSCR1_RxTresh(threshold);
  229. break;
  230. case CE4100_SSP:
  231. *sccr1_reg |= CE4100_SSCR1_RxTresh(threshold);
  232. break;
  233. default:
  234. *sccr1_reg |= SSCR1_RxTresh(threshold);
  235. break;
  236. }
  237. }
  238. static u32 pxa2xx_configure_sscr0(const struct driver_data *drv_data,
  239. u32 clk_div, u8 bits)
  240. {
  241. switch (drv_data->ssp_type) {
  242. case QUARK_X1000_SSP:
  243. return clk_div
  244. | QUARK_X1000_SSCR0_Motorola
  245. | QUARK_X1000_SSCR0_DataSize(bits > 32 ? 8 : bits)
  246. | SSCR0_SSE;
  247. default:
  248. return clk_div
  249. | SSCR0_Motorola
  250. | SSCR0_DataSize(bits > 16 ? bits - 16 : bits)
  251. | SSCR0_SSE
  252. | (bits > 16 ? SSCR0_EDSS : 0);
  253. }
  254. }
  255. /*
  256. * Read and write LPSS SSP private registers. Caller must first check that
  257. * is_lpss_ssp() returns true before these can be called.
  258. */
  259. static u32 __lpss_ssp_read_priv(struct driver_data *drv_data, unsigned offset)
  260. {
  261. WARN_ON(!drv_data->lpss_base);
  262. return readl(drv_data->lpss_base + offset);
  263. }
  264. static void __lpss_ssp_write_priv(struct driver_data *drv_data,
  265. unsigned offset, u32 value)
  266. {
  267. WARN_ON(!drv_data->lpss_base);
  268. writel(value, drv_data->lpss_base + offset);
  269. }
  270. /*
  271. * lpss_ssp_setup - perform LPSS SSP specific setup
  272. * @drv_data: pointer to the driver private data
  273. *
  274. * Perform LPSS SSP specific setup. This function must be called first if
  275. * one is going to use LPSS SSP private registers.
  276. */
  277. static void lpss_ssp_setup(struct driver_data *drv_data)
  278. {
  279. const struct lpss_config *config;
  280. u32 value;
  281. config = lpss_get_config(drv_data);
  282. drv_data->lpss_base = drv_data->ioaddr + config->offset;
  283. /* Enable software chip select control */
  284. value = __lpss_ssp_read_priv(drv_data, config->reg_cs_ctrl);
  285. value &= ~(LPSS_CS_CONTROL_SW_MODE | LPSS_CS_CONTROL_CS_HIGH);
  286. value |= LPSS_CS_CONTROL_SW_MODE | LPSS_CS_CONTROL_CS_HIGH;
  287. __lpss_ssp_write_priv(drv_data, config->reg_cs_ctrl, value);
  288. /* Enable multiblock DMA transfers */
  289. if (drv_data->master_info->enable_dma) {
  290. __lpss_ssp_write_priv(drv_data, config->reg_ssp, 1);
  291. if (config->reg_general >= 0) {
  292. value = __lpss_ssp_read_priv(drv_data,
  293. config->reg_general);
  294. value |= LPSS_GENERAL_REG_RXTO_HOLDOFF_DISABLE;
  295. __lpss_ssp_write_priv(drv_data,
  296. config->reg_general, value);
  297. }
  298. }
  299. }
  300. static void lpss_ssp_select_cs(struct driver_data *drv_data,
  301. const struct lpss_config *config)
  302. {
  303. u32 value, cs;
  304. if (!config->cs_sel_mask)
  305. return;
  306. value = __lpss_ssp_read_priv(drv_data, config->reg_cs_ctrl);
  307. cs = drv_data->master->cur_msg->spi->chip_select;
  308. cs <<= config->cs_sel_shift;
  309. if (cs != (value & config->cs_sel_mask)) {
  310. /*
  311. * When switching another chip select output active the
  312. * output must be selected first and wait 2 ssp_clk cycles
  313. * before changing state to active. Otherwise a short
  314. * glitch will occur on the previous chip select since
  315. * output select is latched but state control is not.
  316. */
  317. value &= ~config->cs_sel_mask;
  318. value |= cs;
  319. __lpss_ssp_write_priv(drv_data,
  320. config->reg_cs_ctrl, value);
  321. ndelay(1000000000 /
  322. (drv_data->master->max_speed_hz / 2));
  323. }
  324. }
  325. static void lpss_ssp_cs_control(struct driver_data *drv_data, bool enable)
  326. {
  327. const struct lpss_config *config;
  328. u32 value;
  329. config = lpss_get_config(drv_data);
  330. if (enable)
  331. lpss_ssp_select_cs(drv_data, config);
  332. value = __lpss_ssp_read_priv(drv_data, config->reg_cs_ctrl);
  333. if (enable)
  334. value &= ~LPSS_CS_CONTROL_CS_HIGH;
  335. else
  336. value |= LPSS_CS_CONTROL_CS_HIGH;
  337. __lpss_ssp_write_priv(drv_data, config->reg_cs_ctrl, value);
  338. }
  339. static void cs_assert(struct driver_data *drv_data)
  340. {
  341. struct chip_data *chip =
  342. spi_get_ctldata(drv_data->master->cur_msg->spi);
  343. if (drv_data->ssp_type == CE4100_SSP) {
  344. pxa2xx_spi_write(drv_data, SSSR, chip->frm);
  345. return;
  346. }
  347. if (chip->cs_control) {
  348. chip->cs_control(PXA2XX_CS_ASSERT);
  349. return;
  350. }
  351. if (gpio_is_valid(chip->gpio_cs)) {
  352. gpio_set_value(chip->gpio_cs, chip->gpio_cs_inverted);
  353. return;
  354. }
  355. if (is_lpss_ssp(drv_data))
  356. lpss_ssp_cs_control(drv_data, true);
  357. }
  358. static void cs_deassert(struct driver_data *drv_data)
  359. {
  360. struct chip_data *chip =
  361. spi_get_ctldata(drv_data->master->cur_msg->spi);
  362. if (drv_data->ssp_type == CE4100_SSP)
  363. return;
  364. if (chip->cs_control) {
  365. chip->cs_control(PXA2XX_CS_DEASSERT);
  366. return;
  367. }
  368. if (gpio_is_valid(chip->gpio_cs)) {
  369. gpio_set_value(chip->gpio_cs, !chip->gpio_cs_inverted);
  370. return;
  371. }
  372. if (is_lpss_ssp(drv_data))
  373. lpss_ssp_cs_control(drv_data, false);
  374. }
  375. int pxa2xx_spi_flush(struct driver_data *drv_data)
  376. {
  377. unsigned long limit = loops_per_jiffy << 1;
  378. do {
  379. while (pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
  380. pxa2xx_spi_read(drv_data, SSDR);
  381. } while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_BSY) && --limit);
  382. write_SSSR_CS(drv_data, SSSR_ROR);
  383. return limit;
  384. }
  385. static int null_writer(struct driver_data *drv_data)
  386. {
  387. u8 n_bytes = drv_data->n_bytes;
  388. if (pxa2xx_spi_txfifo_full(drv_data)
  389. || (drv_data->tx == drv_data->tx_end))
  390. return 0;
  391. pxa2xx_spi_write(drv_data, SSDR, 0);
  392. drv_data->tx += n_bytes;
  393. return 1;
  394. }
  395. static int null_reader(struct driver_data *drv_data)
  396. {
  397. u8 n_bytes = drv_data->n_bytes;
  398. while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
  399. && (drv_data->rx < drv_data->rx_end)) {
  400. pxa2xx_spi_read(drv_data, SSDR);
  401. drv_data->rx += n_bytes;
  402. }
  403. return drv_data->rx == drv_data->rx_end;
  404. }
  405. static int u8_writer(struct driver_data *drv_data)
  406. {
  407. if (pxa2xx_spi_txfifo_full(drv_data)
  408. || (drv_data->tx == drv_data->tx_end))
  409. return 0;
  410. pxa2xx_spi_write(drv_data, SSDR, *(u8 *)(drv_data->tx));
  411. ++drv_data->tx;
  412. return 1;
  413. }
  414. static int u8_reader(struct driver_data *drv_data)
  415. {
  416. while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
  417. && (drv_data->rx < drv_data->rx_end)) {
  418. *(u8 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR);
  419. ++drv_data->rx;
  420. }
  421. return drv_data->rx == drv_data->rx_end;
  422. }
  423. static int u16_writer(struct driver_data *drv_data)
  424. {
  425. if (pxa2xx_spi_txfifo_full(drv_data)
  426. || (drv_data->tx == drv_data->tx_end))
  427. return 0;
  428. pxa2xx_spi_write(drv_data, SSDR, *(u16 *)(drv_data->tx));
  429. drv_data->tx += 2;
  430. return 1;
  431. }
  432. static int u16_reader(struct driver_data *drv_data)
  433. {
  434. while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
  435. && (drv_data->rx < drv_data->rx_end)) {
  436. *(u16 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR);
  437. drv_data->rx += 2;
  438. }
  439. return drv_data->rx == drv_data->rx_end;
  440. }
  441. static int u32_writer(struct driver_data *drv_data)
  442. {
  443. if (pxa2xx_spi_txfifo_full(drv_data)
  444. || (drv_data->tx == drv_data->tx_end))
  445. return 0;
  446. pxa2xx_spi_write(drv_data, SSDR, *(u32 *)(drv_data->tx));
  447. drv_data->tx += 4;
  448. return 1;
  449. }
  450. static int u32_reader(struct driver_data *drv_data)
  451. {
  452. while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
  453. && (drv_data->rx < drv_data->rx_end)) {
  454. *(u32 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR);
  455. drv_data->rx += 4;
  456. }
  457. return drv_data->rx == drv_data->rx_end;
  458. }
  459. void *pxa2xx_spi_next_transfer(struct driver_data *drv_data)
  460. {
  461. struct spi_message *msg = drv_data->master->cur_msg;
  462. struct spi_transfer *trans = drv_data->cur_transfer;
  463. /* Move to next transfer */
  464. if (trans->transfer_list.next != &msg->transfers) {
  465. drv_data->cur_transfer =
  466. list_entry(trans->transfer_list.next,
  467. struct spi_transfer,
  468. transfer_list);
  469. return RUNNING_STATE;
  470. } else
  471. return DONE_STATE;
  472. }
  473. /* caller already set message->status; dma and pio irqs are blocked */
  474. static void giveback(struct driver_data *drv_data)
  475. {
  476. struct spi_transfer* last_transfer;
  477. struct spi_message *msg;
  478. unsigned long timeout;
  479. msg = drv_data->master->cur_msg;
  480. drv_data->cur_transfer = NULL;
  481. last_transfer = list_last_entry(&msg->transfers, struct spi_transfer,
  482. transfer_list);
  483. /* Delay if requested before any change in chip select */
  484. if (last_transfer->delay_usecs)
  485. udelay(last_transfer->delay_usecs);
  486. /* Wait until SSP becomes idle before deasserting the CS */
  487. timeout = jiffies + msecs_to_jiffies(10);
  488. while (pxa2xx_spi_read(drv_data, SSSR) & SSSR_BSY &&
  489. !time_after(jiffies, timeout))
  490. cpu_relax();
  491. /* Drop chip select UNLESS cs_change is true or we are returning
  492. * a message with an error, or next message is for another chip
  493. */
  494. if (!last_transfer->cs_change)
  495. cs_deassert(drv_data);
  496. else {
  497. struct spi_message *next_msg;
  498. /* Holding of cs was hinted, but we need to make sure
  499. * the next message is for the same chip. Don't waste
  500. * time with the following tests unless this was hinted.
  501. *
  502. * We cannot postpone this until pump_messages, because
  503. * after calling msg->complete (below) the driver that
  504. * sent the current message could be unloaded, which
  505. * could invalidate the cs_control() callback...
  506. */
  507. /* get a pointer to the next message, if any */
  508. next_msg = spi_get_next_queued_message(drv_data->master);
  509. /* see if the next and current messages point
  510. * to the same chip
  511. */
  512. if ((next_msg && next_msg->spi != msg->spi) ||
  513. msg->state == ERROR_STATE)
  514. cs_deassert(drv_data);
  515. }
  516. spi_finalize_current_message(drv_data->master);
  517. }
  518. static void reset_sccr1(struct driver_data *drv_data)
  519. {
  520. struct chip_data *chip =
  521. spi_get_ctldata(drv_data->master->cur_msg->spi);
  522. u32 sccr1_reg;
  523. sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1) & ~drv_data->int_cr1;
  524. switch (drv_data->ssp_type) {
  525. case QUARK_X1000_SSP:
  526. sccr1_reg &= ~QUARK_X1000_SSCR1_RFT;
  527. break;
  528. case CE4100_SSP:
  529. sccr1_reg &= ~CE4100_SSCR1_RFT;
  530. break;
  531. default:
  532. sccr1_reg &= ~SSCR1_RFT;
  533. break;
  534. }
  535. sccr1_reg |= chip->threshold;
  536. pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg);
  537. }
  538. static void int_error_stop(struct driver_data *drv_data, const char* msg)
  539. {
  540. /* Stop and reset SSP */
  541. write_SSSR_CS(drv_data, drv_data->clear_sr);
  542. reset_sccr1(drv_data);
  543. if (!pxa25x_ssp_comp(drv_data))
  544. pxa2xx_spi_write(drv_data, SSTO, 0);
  545. pxa2xx_spi_flush(drv_data);
  546. pxa2xx_spi_write(drv_data, SSCR0,
  547. pxa2xx_spi_read(drv_data, SSCR0) & ~SSCR0_SSE);
  548. dev_err(&drv_data->pdev->dev, "%s\n", msg);
  549. drv_data->master->cur_msg->state = ERROR_STATE;
  550. tasklet_schedule(&drv_data->pump_transfers);
  551. }
  552. static void int_transfer_complete(struct driver_data *drv_data)
  553. {
  554. /* Clear and disable interrupts */
  555. write_SSSR_CS(drv_data, drv_data->clear_sr);
  556. reset_sccr1(drv_data);
  557. if (!pxa25x_ssp_comp(drv_data))
  558. pxa2xx_spi_write(drv_data, SSTO, 0);
  559. /* Update total byte transferred return count actual bytes read */
  560. drv_data->master->cur_msg->actual_length += drv_data->len -
  561. (drv_data->rx_end - drv_data->rx);
  562. /* Transfer delays and chip select release are
  563. * handled in pump_transfers or giveback
  564. */
  565. /* Move to next transfer */
  566. drv_data->master->cur_msg->state = pxa2xx_spi_next_transfer(drv_data);
  567. /* Schedule transfer tasklet */
  568. tasklet_schedule(&drv_data->pump_transfers);
  569. }
  570. static irqreturn_t interrupt_transfer(struct driver_data *drv_data)
  571. {
  572. u32 irq_mask = (pxa2xx_spi_read(drv_data, SSCR1) & SSCR1_TIE) ?
  573. drv_data->mask_sr : drv_data->mask_sr & ~SSSR_TFS;
  574. u32 irq_status = pxa2xx_spi_read(drv_data, SSSR) & irq_mask;
  575. if (irq_status & SSSR_ROR) {
  576. int_error_stop(drv_data, "interrupt_transfer: fifo overrun");
  577. return IRQ_HANDLED;
  578. }
  579. if (irq_status & SSSR_TINT) {
  580. pxa2xx_spi_write(drv_data, SSSR, SSSR_TINT);
  581. if (drv_data->read(drv_data)) {
  582. int_transfer_complete(drv_data);
  583. return IRQ_HANDLED;
  584. }
  585. }
  586. /* Drain rx fifo, Fill tx fifo and prevent overruns */
  587. do {
  588. if (drv_data->read(drv_data)) {
  589. int_transfer_complete(drv_data);
  590. return IRQ_HANDLED;
  591. }
  592. } while (drv_data->write(drv_data));
  593. if (drv_data->read(drv_data)) {
  594. int_transfer_complete(drv_data);
  595. return IRQ_HANDLED;
  596. }
  597. if (drv_data->tx == drv_data->tx_end) {
  598. u32 bytes_left;
  599. u32 sccr1_reg;
  600. sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1);
  601. sccr1_reg &= ~SSCR1_TIE;
  602. /*
  603. * PXA25x_SSP has no timeout, set up rx threshould for the
  604. * remaining RX bytes.
  605. */
  606. if (pxa25x_ssp_comp(drv_data)) {
  607. u32 rx_thre;
  608. pxa2xx_spi_clear_rx_thre(drv_data, &sccr1_reg);
  609. bytes_left = drv_data->rx_end - drv_data->rx;
  610. switch (drv_data->n_bytes) {
  611. case 4:
  612. bytes_left >>= 1;
  613. case 2:
  614. bytes_left >>= 1;
  615. }
  616. rx_thre = pxa2xx_spi_get_rx_default_thre(drv_data);
  617. if (rx_thre > bytes_left)
  618. rx_thre = bytes_left;
  619. pxa2xx_spi_set_rx_thre(drv_data, &sccr1_reg, rx_thre);
  620. }
  621. pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg);
  622. }
  623. /* We did something */
  624. return IRQ_HANDLED;
  625. }
  626. static irqreturn_t ssp_int(int irq, void *dev_id)
  627. {
  628. struct driver_data *drv_data = dev_id;
  629. u32 sccr1_reg;
  630. u32 mask = drv_data->mask_sr;
  631. u32 status;
  632. /*
  633. * The IRQ might be shared with other peripherals so we must first
  634. * check that are we RPM suspended or not. If we are we assume that
  635. * the IRQ was not for us (we shouldn't be RPM suspended when the
  636. * interrupt is enabled).
  637. */
  638. if (pm_runtime_suspended(&drv_data->pdev->dev))
  639. return IRQ_NONE;
  640. /*
  641. * If the device is not yet in RPM suspended state and we get an
  642. * interrupt that is meant for another device, check if status bits
  643. * are all set to one. That means that the device is already
  644. * powered off.
  645. */
  646. status = pxa2xx_spi_read(drv_data, SSSR);
  647. if (status == ~0)
  648. return IRQ_NONE;
  649. sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1);
  650. /* Ignore possible writes if we don't need to write */
  651. if (!(sccr1_reg & SSCR1_TIE))
  652. mask &= ~SSSR_TFS;
  653. /* Ignore RX timeout interrupt if it is disabled */
  654. if (!(sccr1_reg & SSCR1_TINTE))
  655. mask &= ~SSSR_TINT;
  656. if (!(status & mask))
  657. return IRQ_NONE;
  658. if (!drv_data->master->cur_msg) {
  659. pxa2xx_spi_write(drv_data, SSCR0,
  660. pxa2xx_spi_read(drv_data, SSCR0)
  661. & ~SSCR0_SSE);
  662. pxa2xx_spi_write(drv_data, SSCR1,
  663. pxa2xx_spi_read(drv_data, SSCR1)
  664. & ~drv_data->int_cr1);
  665. if (!pxa25x_ssp_comp(drv_data))
  666. pxa2xx_spi_write(drv_data, SSTO, 0);
  667. write_SSSR_CS(drv_data, drv_data->clear_sr);
  668. dev_err(&drv_data->pdev->dev,
  669. "bad message state in interrupt handler\n");
  670. /* Never fail */
  671. return IRQ_HANDLED;
  672. }
  673. return drv_data->transfer_handler(drv_data);
  674. }
  675. /*
  676. * The Quark SPI has an additional 24 bit register (DDS_CLK_RATE) to multiply
  677. * input frequency by fractions of 2^24. It also has a divider by 5.
  678. *
  679. * There are formulas to get baud rate value for given input frequency and
  680. * divider parameters, such as DDS_CLK_RATE and SCR:
  681. *
  682. * Fsys = 200MHz
  683. *
  684. * Fssp = Fsys * DDS_CLK_RATE / 2^24 (1)
  685. * Baud rate = Fsclk = Fssp / (2 * (SCR + 1)) (2)
  686. *
  687. * DDS_CLK_RATE either 2^n or 2^n / 5.
  688. * SCR is in range 0 .. 255
  689. *
  690. * Divisor = 5^i * 2^j * 2 * k
  691. * i = [0, 1] i = 1 iff j = 0 or j > 3
  692. * j = [0, 23] j = 0 iff i = 1
  693. * k = [1, 256]
  694. * Special case: j = 0, i = 1: Divisor = 2 / 5
  695. *
  696. * Accordingly to the specification the recommended values for DDS_CLK_RATE
  697. * are:
  698. * Case 1: 2^n, n = [0, 23]
  699. * Case 2: 2^24 * 2 / 5 (0x666666)
  700. * Case 3: less than or equal to 2^24 / 5 / 16 (0x33333)
  701. *
  702. * In all cases the lowest possible value is better.
  703. *
  704. * The function calculates parameters for all cases and chooses the one closest
  705. * to the asked baud rate.
  706. */
  707. static unsigned int quark_x1000_get_clk_div(int rate, u32 *dds)
  708. {
  709. unsigned long xtal = 200000000;
  710. unsigned long fref = xtal / 2; /* mandatory division by 2,
  711. see (2) */
  712. /* case 3 */
  713. unsigned long fref1 = fref / 2; /* case 1 */
  714. unsigned long fref2 = fref * 2 / 5; /* case 2 */
  715. unsigned long scale;
  716. unsigned long q, q1, q2;
  717. long r, r1, r2;
  718. u32 mul;
  719. /* Case 1 */
  720. /* Set initial value for DDS_CLK_RATE */
  721. mul = (1 << 24) >> 1;
  722. /* Calculate initial quot */
  723. q1 = DIV_ROUND_UP(fref1, rate);
  724. /* Scale q1 if it's too big */
  725. if (q1 > 256) {
  726. /* Scale q1 to range [1, 512] */
  727. scale = fls_long(q1 - 1);
  728. if (scale > 9) {
  729. q1 >>= scale - 9;
  730. mul >>= scale - 9;
  731. }
  732. /* Round the result if we have a remainder */
  733. q1 += q1 & 1;
  734. }
  735. /* Decrease DDS_CLK_RATE as much as we can without loss in precision */
  736. scale = __ffs(q1);
  737. q1 >>= scale;
  738. mul >>= scale;
  739. /* Get the remainder */
  740. r1 = abs(fref1 / (1 << (24 - fls_long(mul))) / q1 - rate);
  741. /* Case 2 */
  742. q2 = DIV_ROUND_UP(fref2, rate);
  743. r2 = abs(fref2 / q2 - rate);
  744. /*
  745. * Choose the best between two: less remainder we have the better. We
  746. * can't go case 2 if q2 is greater than 256 since SCR register can
  747. * hold only values 0 .. 255.
  748. */
  749. if (r2 >= r1 || q2 > 256) {
  750. /* case 1 is better */
  751. r = r1;
  752. q = q1;
  753. } else {
  754. /* case 2 is better */
  755. r = r2;
  756. q = q2;
  757. mul = (1 << 24) * 2 / 5;
  758. }
  759. /* Check case 3 only if the divisor is big enough */
  760. if (fref / rate >= 80) {
  761. u64 fssp;
  762. u32 m;
  763. /* Calculate initial quot */
  764. q1 = DIV_ROUND_UP(fref, rate);
  765. m = (1 << 24) / q1;
  766. /* Get the remainder */
  767. fssp = (u64)fref * m;
  768. do_div(fssp, 1 << 24);
  769. r1 = abs(fssp - rate);
  770. /* Choose this one if it suits better */
  771. if (r1 < r) {
  772. /* case 3 is better */
  773. q = 1;
  774. mul = m;
  775. }
  776. }
  777. *dds = mul;
  778. return q - 1;
  779. }
  780. static unsigned int ssp_get_clk_div(struct driver_data *drv_data, int rate)
  781. {
  782. unsigned long ssp_clk = drv_data->master->max_speed_hz;
  783. const struct ssp_device *ssp = drv_data->ssp;
  784. rate = min_t(int, ssp_clk, rate);
  785. if (ssp->type == PXA25x_SSP || ssp->type == CE4100_SSP)
  786. return (ssp_clk / (2 * rate) - 1) & 0xff;
  787. else
  788. return (ssp_clk / rate - 1) & 0xfff;
  789. }
  790. static unsigned int pxa2xx_ssp_get_clk_div(struct driver_data *drv_data,
  791. int rate)
  792. {
  793. struct chip_data *chip =
  794. spi_get_ctldata(drv_data->master->cur_msg->spi);
  795. unsigned int clk_div;
  796. switch (drv_data->ssp_type) {
  797. case QUARK_X1000_SSP:
  798. clk_div = quark_x1000_get_clk_div(rate, &chip->dds_rate);
  799. break;
  800. default:
  801. clk_div = ssp_get_clk_div(drv_data, rate);
  802. break;
  803. }
  804. return clk_div << 8;
  805. }
  806. static bool pxa2xx_spi_can_dma(struct spi_master *master,
  807. struct spi_device *spi,
  808. struct spi_transfer *xfer)
  809. {
  810. struct chip_data *chip = spi_get_ctldata(spi);
  811. return chip->enable_dma &&
  812. xfer->len <= MAX_DMA_LEN &&
  813. xfer->len >= chip->dma_burst_size;
  814. }
  815. static void pump_transfers(unsigned long data)
  816. {
  817. struct driver_data *drv_data = (struct driver_data *)data;
  818. struct spi_master *master = drv_data->master;
  819. struct spi_message *message = master->cur_msg;
  820. struct chip_data *chip = spi_get_ctldata(message->spi);
  821. u32 dma_thresh = chip->dma_threshold;
  822. u32 dma_burst = chip->dma_burst_size;
  823. u32 change_mask = pxa2xx_spi_get_ssrc1_change_mask(drv_data);
  824. struct spi_transfer *transfer;
  825. struct spi_transfer *previous;
  826. u32 clk_div;
  827. u8 bits;
  828. u32 speed;
  829. u32 cr0;
  830. u32 cr1;
  831. int err;
  832. int dma_mapped;
  833. /* Get current state information */
  834. transfer = drv_data->cur_transfer;
  835. /* Handle for abort */
  836. if (message->state == ERROR_STATE) {
  837. message->status = -EIO;
  838. giveback(drv_data);
  839. return;
  840. }
  841. /* Handle end of message */
  842. if (message->state == DONE_STATE) {
  843. message->status = 0;
  844. giveback(drv_data);
  845. return;
  846. }
  847. /* Delay if requested at end of transfer before CS change */
  848. if (message->state == RUNNING_STATE) {
  849. previous = list_entry(transfer->transfer_list.prev,
  850. struct spi_transfer,
  851. transfer_list);
  852. if (previous->delay_usecs)
  853. udelay(previous->delay_usecs);
  854. /* Drop chip select only if cs_change is requested */
  855. if (previous->cs_change)
  856. cs_deassert(drv_data);
  857. }
  858. /* Check if we can DMA this transfer */
  859. if (transfer->len > MAX_DMA_LEN && chip->enable_dma) {
  860. /* reject already-mapped transfers; PIO won't always work */
  861. if (message->is_dma_mapped
  862. || transfer->rx_dma || transfer->tx_dma) {
  863. dev_err(&drv_data->pdev->dev,
  864. "pump_transfers: mapped transfer length of "
  865. "%u is greater than %d\n",
  866. transfer->len, MAX_DMA_LEN);
  867. message->status = -EINVAL;
  868. giveback(drv_data);
  869. return;
  870. }
  871. /* warn ... we force this to PIO mode */
  872. dev_warn_ratelimited(&message->spi->dev,
  873. "pump_transfers: DMA disabled for transfer length %ld "
  874. "greater than %d\n",
  875. (long)drv_data->len, MAX_DMA_LEN);
  876. }
  877. /* Setup the transfer state based on the type of transfer */
  878. if (pxa2xx_spi_flush(drv_data) == 0) {
  879. dev_err(&drv_data->pdev->dev, "pump_transfers: flush failed\n");
  880. message->status = -EIO;
  881. giveback(drv_data);
  882. return;
  883. }
  884. drv_data->n_bytes = chip->n_bytes;
  885. drv_data->tx = (void *)transfer->tx_buf;
  886. drv_data->tx_end = drv_data->tx + transfer->len;
  887. drv_data->rx = transfer->rx_buf;
  888. drv_data->rx_end = drv_data->rx + transfer->len;
  889. drv_data->len = transfer->len;
  890. drv_data->write = drv_data->tx ? chip->write : null_writer;
  891. drv_data->read = drv_data->rx ? chip->read : null_reader;
  892. /* Change speed and bit per word on a per transfer */
  893. bits = transfer->bits_per_word;
  894. speed = transfer->speed_hz;
  895. clk_div = pxa2xx_ssp_get_clk_div(drv_data, speed);
  896. if (bits <= 8) {
  897. drv_data->n_bytes = 1;
  898. drv_data->read = drv_data->read != null_reader ?
  899. u8_reader : null_reader;
  900. drv_data->write = drv_data->write != null_writer ?
  901. u8_writer : null_writer;
  902. } else if (bits <= 16) {
  903. drv_data->n_bytes = 2;
  904. drv_data->read = drv_data->read != null_reader ?
  905. u16_reader : null_reader;
  906. drv_data->write = drv_data->write != null_writer ?
  907. u16_writer : null_writer;
  908. } else if (bits <= 32) {
  909. drv_data->n_bytes = 4;
  910. drv_data->read = drv_data->read != null_reader ?
  911. u32_reader : null_reader;
  912. drv_data->write = drv_data->write != null_writer ?
  913. u32_writer : null_writer;
  914. }
  915. /*
  916. * if bits/word is changed in dma mode, then must check the
  917. * thresholds and burst also
  918. */
  919. if (chip->enable_dma) {
  920. if (pxa2xx_spi_set_dma_burst_and_threshold(chip,
  921. message->spi,
  922. bits, &dma_burst,
  923. &dma_thresh))
  924. dev_warn_ratelimited(&message->spi->dev,
  925. "pump_transfers: DMA burst size reduced to match bits_per_word\n");
  926. }
  927. message->state = RUNNING_STATE;
  928. dma_mapped = master->can_dma &&
  929. master->can_dma(master, message->spi, transfer) &&
  930. master->cur_msg_mapped;
  931. if (dma_mapped) {
  932. /* Ensure we have the correct interrupt handler */
  933. drv_data->transfer_handler = pxa2xx_spi_dma_transfer;
  934. err = pxa2xx_spi_dma_prepare(drv_data, dma_burst);
  935. if (err) {
  936. message->status = err;
  937. giveback(drv_data);
  938. return;
  939. }
  940. /* Clear status and start DMA engine */
  941. cr1 = chip->cr1 | dma_thresh | drv_data->dma_cr1;
  942. pxa2xx_spi_write(drv_data, SSSR, drv_data->clear_sr);
  943. pxa2xx_spi_dma_start(drv_data);
  944. } else {
  945. /* Ensure we have the correct interrupt handler */
  946. drv_data->transfer_handler = interrupt_transfer;
  947. /* Clear status */
  948. cr1 = chip->cr1 | chip->threshold | drv_data->int_cr1;
  949. write_SSSR_CS(drv_data, drv_data->clear_sr);
  950. }
  951. /* NOTE: PXA25x_SSP _could_ use external clocking ... */
  952. cr0 = pxa2xx_configure_sscr0(drv_data, clk_div, bits);
  953. if (!pxa25x_ssp_comp(drv_data))
  954. dev_dbg(&message->spi->dev, "%u Hz actual, %s\n",
  955. master->max_speed_hz
  956. / (1 + ((cr0 & SSCR0_SCR(0xfff)) >> 8)),
  957. dma_mapped ? "DMA" : "PIO");
  958. else
  959. dev_dbg(&message->spi->dev, "%u Hz actual, %s\n",
  960. master->max_speed_hz / 2
  961. / (1 + ((cr0 & SSCR0_SCR(0x0ff)) >> 8)),
  962. dma_mapped ? "DMA" : "PIO");
  963. if (is_lpss_ssp(drv_data)) {
  964. if ((pxa2xx_spi_read(drv_data, SSIRF) & 0xff)
  965. != chip->lpss_rx_threshold)
  966. pxa2xx_spi_write(drv_data, SSIRF,
  967. chip->lpss_rx_threshold);
  968. if ((pxa2xx_spi_read(drv_data, SSITF) & 0xffff)
  969. != chip->lpss_tx_threshold)
  970. pxa2xx_spi_write(drv_data, SSITF,
  971. chip->lpss_tx_threshold);
  972. }
  973. if (is_quark_x1000_ssp(drv_data) &&
  974. (pxa2xx_spi_read(drv_data, DDS_RATE) != chip->dds_rate))
  975. pxa2xx_spi_write(drv_data, DDS_RATE, chip->dds_rate);
  976. /* see if we need to reload the config registers */
  977. if ((pxa2xx_spi_read(drv_data, SSCR0) != cr0)
  978. || (pxa2xx_spi_read(drv_data, SSCR1) & change_mask)
  979. != (cr1 & change_mask)) {
  980. /* stop the SSP, and update the other bits */
  981. pxa2xx_spi_write(drv_data, SSCR0, cr0 & ~SSCR0_SSE);
  982. if (!pxa25x_ssp_comp(drv_data))
  983. pxa2xx_spi_write(drv_data, SSTO, chip->timeout);
  984. /* first set CR1 without interrupt and service enables */
  985. pxa2xx_spi_write(drv_data, SSCR1, cr1 & change_mask);
  986. /* restart the SSP */
  987. pxa2xx_spi_write(drv_data, SSCR0, cr0);
  988. } else {
  989. if (!pxa25x_ssp_comp(drv_data))
  990. pxa2xx_spi_write(drv_data, SSTO, chip->timeout);
  991. }
  992. cs_assert(drv_data);
  993. /* after chip select, release the data by enabling service
  994. * requests and interrupts, without changing any mode bits */
  995. pxa2xx_spi_write(drv_data, SSCR1, cr1);
  996. }
  997. static int pxa2xx_spi_transfer_one_message(struct spi_master *master,
  998. struct spi_message *msg)
  999. {
  1000. struct driver_data *drv_data = spi_master_get_devdata(master);
  1001. /* Initial message state*/
  1002. msg->state = START_STATE;
  1003. drv_data->cur_transfer = list_entry(msg->transfers.next,
  1004. struct spi_transfer,
  1005. transfer_list);
  1006. /* Mark as busy and launch transfers */
  1007. tasklet_schedule(&drv_data->pump_transfers);
  1008. return 0;
  1009. }
  1010. static int pxa2xx_spi_unprepare_transfer(struct spi_master *master)
  1011. {
  1012. struct driver_data *drv_data = spi_master_get_devdata(master);
  1013. /* Disable the SSP now */
  1014. pxa2xx_spi_write(drv_data, SSCR0,
  1015. pxa2xx_spi_read(drv_data, SSCR0) & ~SSCR0_SSE);
  1016. return 0;
  1017. }
  1018. static int setup_cs(struct spi_device *spi, struct chip_data *chip,
  1019. struct pxa2xx_spi_chip *chip_info)
  1020. {
  1021. struct driver_data *drv_data = spi_master_get_devdata(spi->master);
  1022. int err = 0;
  1023. if (chip == NULL)
  1024. return 0;
  1025. if (drv_data->cs_gpiods) {
  1026. struct gpio_desc *gpiod;
  1027. gpiod = drv_data->cs_gpiods[spi->chip_select];
  1028. if (gpiod) {
  1029. chip->gpio_cs = desc_to_gpio(gpiod);
  1030. chip->gpio_cs_inverted = spi->mode & SPI_CS_HIGH;
  1031. gpiod_set_value(gpiod, chip->gpio_cs_inverted);
  1032. }
  1033. return 0;
  1034. }
  1035. if (chip_info == NULL)
  1036. return 0;
  1037. /* NOTE: setup() can be called multiple times, possibly with
  1038. * different chip_info, release previously requested GPIO
  1039. */
  1040. if (gpio_is_valid(chip->gpio_cs))
  1041. gpio_free(chip->gpio_cs);
  1042. /* If (*cs_control) is provided, ignore GPIO chip select */
  1043. if (chip_info->cs_control) {
  1044. chip->cs_control = chip_info->cs_control;
  1045. return 0;
  1046. }
  1047. if (gpio_is_valid(chip_info->gpio_cs)) {
  1048. err = gpio_request(chip_info->gpio_cs, "SPI_CS");
  1049. if (err) {
  1050. dev_err(&spi->dev, "failed to request chip select GPIO%d\n",
  1051. chip_info->gpio_cs);
  1052. return err;
  1053. }
  1054. chip->gpio_cs = chip_info->gpio_cs;
  1055. chip->gpio_cs_inverted = spi->mode & SPI_CS_HIGH;
  1056. err = gpio_direction_output(chip->gpio_cs,
  1057. !chip->gpio_cs_inverted);
  1058. }
  1059. return err;
  1060. }
  1061. static int setup(struct spi_device *spi)
  1062. {
  1063. struct pxa2xx_spi_chip *chip_info;
  1064. struct chip_data *chip;
  1065. const struct lpss_config *config;
  1066. struct driver_data *drv_data = spi_master_get_devdata(spi->master);
  1067. uint tx_thres, tx_hi_thres, rx_thres;
  1068. switch (drv_data->ssp_type) {
  1069. case QUARK_X1000_SSP:
  1070. tx_thres = TX_THRESH_QUARK_X1000_DFLT;
  1071. tx_hi_thres = 0;
  1072. rx_thres = RX_THRESH_QUARK_X1000_DFLT;
  1073. break;
  1074. case CE4100_SSP:
  1075. tx_thres = TX_THRESH_CE4100_DFLT;
  1076. tx_hi_thres = 0;
  1077. rx_thres = RX_THRESH_CE4100_DFLT;
  1078. break;
  1079. case LPSS_LPT_SSP:
  1080. case LPSS_BYT_SSP:
  1081. case LPSS_BSW_SSP:
  1082. case LPSS_SPT_SSP:
  1083. case LPSS_BXT_SSP:
  1084. config = lpss_get_config(drv_data);
  1085. tx_thres = config->tx_threshold_lo;
  1086. tx_hi_thres = config->tx_threshold_hi;
  1087. rx_thres = config->rx_threshold;
  1088. break;
  1089. default:
  1090. tx_thres = TX_THRESH_DFLT;
  1091. tx_hi_thres = 0;
  1092. rx_thres = RX_THRESH_DFLT;
  1093. break;
  1094. }
  1095. /* Only alloc on first setup */
  1096. chip = spi_get_ctldata(spi);
  1097. if (!chip) {
  1098. chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL);
  1099. if (!chip)
  1100. return -ENOMEM;
  1101. if (drv_data->ssp_type == CE4100_SSP) {
  1102. if (spi->chip_select > 4) {
  1103. dev_err(&spi->dev,
  1104. "failed setup: cs number must not be > 4.\n");
  1105. kfree(chip);
  1106. return -EINVAL;
  1107. }
  1108. chip->frm = spi->chip_select;
  1109. } else
  1110. chip->gpio_cs = -1;
  1111. chip->enable_dma = drv_data->master_info->enable_dma;
  1112. chip->timeout = TIMOUT_DFLT;
  1113. }
  1114. /* protocol drivers may change the chip settings, so...
  1115. * if chip_info exists, use it */
  1116. chip_info = spi->controller_data;
  1117. /* chip_info isn't always needed */
  1118. chip->cr1 = 0;
  1119. if (chip_info) {
  1120. if (chip_info->timeout)
  1121. chip->timeout = chip_info->timeout;
  1122. if (chip_info->tx_threshold)
  1123. tx_thres = chip_info->tx_threshold;
  1124. if (chip_info->tx_hi_threshold)
  1125. tx_hi_thres = chip_info->tx_hi_threshold;
  1126. if (chip_info->rx_threshold)
  1127. rx_thres = chip_info->rx_threshold;
  1128. chip->dma_threshold = 0;
  1129. if (chip_info->enable_loopback)
  1130. chip->cr1 = SSCR1_LBM;
  1131. }
  1132. chip->lpss_rx_threshold = SSIRF_RxThresh(rx_thres);
  1133. chip->lpss_tx_threshold = SSITF_TxLoThresh(tx_thres)
  1134. | SSITF_TxHiThresh(tx_hi_thres);
  1135. /* set dma burst and threshold outside of chip_info path so that if
  1136. * chip_info goes away after setting chip->enable_dma, the
  1137. * burst and threshold can still respond to changes in bits_per_word */
  1138. if (chip->enable_dma) {
  1139. /* set up legal burst and threshold for dma */
  1140. if (pxa2xx_spi_set_dma_burst_and_threshold(chip, spi,
  1141. spi->bits_per_word,
  1142. &chip->dma_burst_size,
  1143. &chip->dma_threshold)) {
  1144. dev_warn(&spi->dev,
  1145. "in setup: DMA burst size reduced to match bits_per_word\n");
  1146. }
  1147. }
  1148. switch (drv_data->ssp_type) {
  1149. case QUARK_X1000_SSP:
  1150. chip->threshold = (QUARK_X1000_SSCR1_RxTresh(rx_thres)
  1151. & QUARK_X1000_SSCR1_RFT)
  1152. | (QUARK_X1000_SSCR1_TxTresh(tx_thres)
  1153. & QUARK_X1000_SSCR1_TFT);
  1154. break;
  1155. case CE4100_SSP:
  1156. chip->threshold = (CE4100_SSCR1_RxTresh(rx_thres) & CE4100_SSCR1_RFT) |
  1157. (CE4100_SSCR1_TxTresh(tx_thres) & CE4100_SSCR1_TFT);
  1158. break;
  1159. default:
  1160. chip->threshold = (SSCR1_RxTresh(rx_thres) & SSCR1_RFT) |
  1161. (SSCR1_TxTresh(tx_thres) & SSCR1_TFT);
  1162. break;
  1163. }
  1164. chip->cr1 &= ~(SSCR1_SPO | SSCR1_SPH);
  1165. chip->cr1 |= (((spi->mode & SPI_CPHA) != 0) ? SSCR1_SPH : 0)
  1166. | (((spi->mode & SPI_CPOL) != 0) ? SSCR1_SPO : 0);
  1167. if (spi->mode & SPI_LOOP)
  1168. chip->cr1 |= SSCR1_LBM;
  1169. if (spi->bits_per_word <= 8) {
  1170. chip->n_bytes = 1;
  1171. chip->read = u8_reader;
  1172. chip->write = u8_writer;
  1173. } else if (spi->bits_per_word <= 16) {
  1174. chip->n_bytes = 2;
  1175. chip->read = u16_reader;
  1176. chip->write = u16_writer;
  1177. } else if (spi->bits_per_word <= 32) {
  1178. chip->n_bytes = 4;
  1179. chip->read = u32_reader;
  1180. chip->write = u32_writer;
  1181. }
  1182. spi_set_ctldata(spi, chip);
  1183. if (drv_data->ssp_type == CE4100_SSP)
  1184. return 0;
  1185. return setup_cs(spi, chip, chip_info);
  1186. }
  1187. static void cleanup(struct spi_device *spi)
  1188. {
  1189. struct chip_data *chip = spi_get_ctldata(spi);
  1190. struct driver_data *drv_data = spi_master_get_devdata(spi->master);
  1191. if (!chip)
  1192. return;
  1193. if (drv_data->ssp_type != CE4100_SSP && !drv_data->cs_gpiods &&
  1194. gpio_is_valid(chip->gpio_cs))
  1195. gpio_free(chip->gpio_cs);
  1196. kfree(chip);
  1197. }
  1198. #ifdef CONFIG_PCI
  1199. #ifdef CONFIG_ACPI
  1200. static const struct acpi_device_id pxa2xx_spi_acpi_match[] = {
  1201. { "INT33C0", LPSS_LPT_SSP },
  1202. { "INT33C1", LPSS_LPT_SSP },
  1203. { "INT3430", LPSS_LPT_SSP },
  1204. { "INT3431", LPSS_LPT_SSP },
  1205. { "80860F0E", LPSS_BYT_SSP },
  1206. { "8086228E", LPSS_BSW_SSP },
  1207. { },
  1208. };
  1209. MODULE_DEVICE_TABLE(acpi, pxa2xx_spi_acpi_match);
  1210. static int pxa2xx_spi_get_port_id(struct acpi_device *adev)
  1211. {
  1212. unsigned int devid;
  1213. int port_id = -1;
  1214. if (adev && adev->pnp.unique_id &&
  1215. !kstrtouint(adev->pnp.unique_id, 0, &devid))
  1216. port_id = devid;
  1217. return port_id;
  1218. }
  1219. #else /* !CONFIG_ACPI */
  1220. static int pxa2xx_spi_get_port_id(struct acpi_device *adev)
  1221. {
  1222. return -1;
  1223. }
  1224. #endif
  1225. /*
  1226. * PCI IDs of compound devices that integrate both host controller and private
  1227. * integrated DMA engine. Please note these are not used in module
  1228. * autoloading and probing in this module but matching the LPSS SSP type.
  1229. */
  1230. static const struct pci_device_id pxa2xx_spi_pci_compound_match[] = {
  1231. /* SPT-LP */
  1232. { PCI_VDEVICE(INTEL, 0x9d29), LPSS_SPT_SSP },
  1233. { PCI_VDEVICE(INTEL, 0x9d2a), LPSS_SPT_SSP },
  1234. /* SPT-H */
  1235. { PCI_VDEVICE(INTEL, 0xa129), LPSS_SPT_SSP },
  1236. { PCI_VDEVICE(INTEL, 0xa12a), LPSS_SPT_SSP },
  1237. /* KBL-H */
  1238. { PCI_VDEVICE(INTEL, 0xa2a9), LPSS_SPT_SSP },
  1239. { PCI_VDEVICE(INTEL, 0xa2aa), LPSS_SPT_SSP },
  1240. /* BXT A-Step */
  1241. { PCI_VDEVICE(INTEL, 0x0ac2), LPSS_BXT_SSP },
  1242. { PCI_VDEVICE(INTEL, 0x0ac4), LPSS_BXT_SSP },
  1243. { PCI_VDEVICE(INTEL, 0x0ac6), LPSS_BXT_SSP },
  1244. /* BXT B-Step */
  1245. { PCI_VDEVICE(INTEL, 0x1ac2), LPSS_BXT_SSP },
  1246. { PCI_VDEVICE(INTEL, 0x1ac4), LPSS_BXT_SSP },
  1247. { PCI_VDEVICE(INTEL, 0x1ac6), LPSS_BXT_SSP },
  1248. /* GLK */
  1249. { PCI_VDEVICE(INTEL, 0x31c2), LPSS_BXT_SSP },
  1250. { PCI_VDEVICE(INTEL, 0x31c4), LPSS_BXT_SSP },
  1251. { PCI_VDEVICE(INTEL, 0x31c6), LPSS_BXT_SSP },
  1252. /* APL */
  1253. { PCI_VDEVICE(INTEL, 0x5ac2), LPSS_BXT_SSP },
  1254. { PCI_VDEVICE(INTEL, 0x5ac4), LPSS_BXT_SSP },
  1255. { PCI_VDEVICE(INTEL, 0x5ac6), LPSS_BXT_SSP },
  1256. { },
  1257. };
  1258. static bool pxa2xx_spi_idma_filter(struct dma_chan *chan, void *param)
  1259. {
  1260. struct device *dev = param;
  1261. if (dev != chan->device->dev->parent)
  1262. return false;
  1263. return true;
  1264. }
  1265. static struct pxa2xx_spi_master *
  1266. pxa2xx_spi_init_pdata(struct platform_device *pdev)
  1267. {
  1268. struct pxa2xx_spi_master *pdata;
  1269. struct acpi_device *adev;
  1270. struct ssp_device *ssp;
  1271. struct resource *res;
  1272. const struct acpi_device_id *adev_id = NULL;
  1273. const struct pci_device_id *pcidev_id = NULL;
  1274. int type;
  1275. adev = ACPI_COMPANION(&pdev->dev);
  1276. if (dev_is_pci(pdev->dev.parent))
  1277. pcidev_id = pci_match_id(pxa2xx_spi_pci_compound_match,
  1278. to_pci_dev(pdev->dev.parent));
  1279. else if (adev)
  1280. adev_id = acpi_match_device(pdev->dev.driver->acpi_match_table,
  1281. &pdev->dev);
  1282. else
  1283. return NULL;
  1284. if (adev_id)
  1285. type = (int)adev_id->driver_data;
  1286. else if (pcidev_id)
  1287. type = (int)pcidev_id->driver_data;
  1288. else
  1289. return NULL;
  1290. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  1291. if (!pdata)
  1292. return NULL;
  1293. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1294. if (!res)
  1295. return NULL;
  1296. ssp = &pdata->ssp;
  1297. ssp->phys_base = res->start;
  1298. ssp->mmio_base = devm_ioremap_resource(&pdev->dev, res);
  1299. if (IS_ERR(ssp->mmio_base))
  1300. return NULL;
  1301. if (pcidev_id) {
  1302. pdata->tx_param = pdev->dev.parent;
  1303. pdata->rx_param = pdev->dev.parent;
  1304. pdata->dma_filter = pxa2xx_spi_idma_filter;
  1305. }
  1306. ssp->clk = devm_clk_get(&pdev->dev, NULL);
  1307. ssp->irq = platform_get_irq(pdev, 0);
  1308. ssp->type = type;
  1309. ssp->pdev = pdev;
  1310. ssp->port_id = pxa2xx_spi_get_port_id(adev);
  1311. pdata->num_chipselect = 1;
  1312. pdata->enable_dma = true;
  1313. return pdata;
  1314. }
  1315. #else /* !CONFIG_PCI */
  1316. static inline struct pxa2xx_spi_master *
  1317. pxa2xx_spi_init_pdata(struct platform_device *pdev)
  1318. {
  1319. return NULL;
  1320. }
  1321. #endif
  1322. static int pxa2xx_spi_fw_translate_cs(struct spi_master *master, unsigned cs)
  1323. {
  1324. struct driver_data *drv_data = spi_master_get_devdata(master);
  1325. if (has_acpi_companion(&drv_data->pdev->dev)) {
  1326. switch (drv_data->ssp_type) {
  1327. /*
  1328. * For Atoms the ACPI DeviceSelection used by the Windows
  1329. * driver starts from 1 instead of 0 so translate it here
  1330. * to match what Linux expects.
  1331. */
  1332. case LPSS_BYT_SSP:
  1333. case LPSS_BSW_SSP:
  1334. return cs - 1;
  1335. default:
  1336. break;
  1337. }
  1338. }
  1339. return cs;
  1340. }
  1341. static int pxa2xx_spi_probe(struct platform_device *pdev)
  1342. {
  1343. struct device *dev = &pdev->dev;
  1344. struct pxa2xx_spi_master *platform_info;
  1345. struct spi_master *master;
  1346. struct driver_data *drv_data;
  1347. struct ssp_device *ssp;
  1348. const struct lpss_config *config;
  1349. int status, count;
  1350. u32 tmp;
  1351. platform_info = dev_get_platdata(dev);
  1352. if (!platform_info) {
  1353. platform_info = pxa2xx_spi_init_pdata(pdev);
  1354. if (!platform_info) {
  1355. dev_err(&pdev->dev, "missing platform data\n");
  1356. return -ENODEV;
  1357. }
  1358. }
  1359. ssp = pxa_ssp_request(pdev->id, pdev->name);
  1360. if (!ssp)
  1361. ssp = &platform_info->ssp;
  1362. if (!ssp->mmio_base) {
  1363. dev_err(&pdev->dev, "failed to get ssp\n");
  1364. return -ENODEV;
  1365. }
  1366. master = spi_alloc_master(dev, sizeof(struct driver_data));
  1367. if (!master) {
  1368. dev_err(&pdev->dev, "cannot alloc spi_master\n");
  1369. pxa_ssp_free(ssp);
  1370. return -ENOMEM;
  1371. }
  1372. drv_data = spi_master_get_devdata(master);
  1373. drv_data->master = master;
  1374. drv_data->master_info = platform_info;
  1375. drv_data->pdev = pdev;
  1376. drv_data->ssp = ssp;
  1377. master->dev.of_node = pdev->dev.of_node;
  1378. /* the spi->mode bits understood by this driver: */
  1379. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_LOOP;
  1380. master->bus_num = ssp->port_id;
  1381. master->dma_alignment = DMA_ALIGNMENT;
  1382. master->cleanup = cleanup;
  1383. master->setup = setup;
  1384. master->transfer_one_message = pxa2xx_spi_transfer_one_message;
  1385. master->unprepare_transfer_hardware = pxa2xx_spi_unprepare_transfer;
  1386. master->fw_translate_cs = pxa2xx_spi_fw_translate_cs;
  1387. master->auto_runtime_pm = true;
  1388. master->flags = SPI_MASTER_MUST_RX | SPI_MASTER_MUST_TX;
  1389. drv_data->ssp_type = ssp->type;
  1390. drv_data->ioaddr = ssp->mmio_base;
  1391. drv_data->ssdr_physical = ssp->phys_base + SSDR;
  1392. if (pxa25x_ssp_comp(drv_data)) {
  1393. switch (drv_data->ssp_type) {
  1394. case QUARK_X1000_SSP:
  1395. master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
  1396. break;
  1397. default:
  1398. master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 16);
  1399. break;
  1400. }
  1401. drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE;
  1402. drv_data->dma_cr1 = 0;
  1403. drv_data->clear_sr = SSSR_ROR;
  1404. drv_data->mask_sr = SSSR_RFS | SSSR_TFS | SSSR_ROR;
  1405. } else {
  1406. master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
  1407. drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE | SSCR1_TINTE;
  1408. drv_data->dma_cr1 = DEFAULT_DMA_CR1;
  1409. drv_data->clear_sr = SSSR_ROR | SSSR_TINT;
  1410. drv_data->mask_sr = SSSR_TINT | SSSR_RFS | SSSR_TFS | SSSR_ROR;
  1411. }
  1412. status = request_irq(ssp->irq, ssp_int, IRQF_SHARED, dev_name(dev),
  1413. drv_data);
  1414. if (status < 0) {
  1415. dev_err(&pdev->dev, "cannot get IRQ %d\n", ssp->irq);
  1416. goto out_error_master_alloc;
  1417. }
  1418. /* Setup DMA if requested */
  1419. if (platform_info->enable_dma) {
  1420. status = pxa2xx_spi_dma_setup(drv_data);
  1421. if (status) {
  1422. dev_dbg(dev, "no DMA channels available, using PIO\n");
  1423. platform_info->enable_dma = false;
  1424. } else {
  1425. master->can_dma = pxa2xx_spi_can_dma;
  1426. }
  1427. }
  1428. /* Enable SOC clock */
  1429. clk_prepare_enable(ssp->clk);
  1430. master->max_speed_hz = clk_get_rate(ssp->clk);
  1431. /* Load default SSP configuration */
  1432. pxa2xx_spi_write(drv_data, SSCR0, 0);
  1433. switch (drv_data->ssp_type) {
  1434. case QUARK_X1000_SSP:
  1435. tmp = QUARK_X1000_SSCR1_RxTresh(RX_THRESH_QUARK_X1000_DFLT) |
  1436. QUARK_X1000_SSCR1_TxTresh(TX_THRESH_QUARK_X1000_DFLT);
  1437. pxa2xx_spi_write(drv_data, SSCR1, tmp);
  1438. /* using the Motorola SPI protocol and use 8 bit frame */
  1439. tmp = QUARK_X1000_SSCR0_Motorola | QUARK_X1000_SSCR0_DataSize(8);
  1440. pxa2xx_spi_write(drv_data, SSCR0, tmp);
  1441. break;
  1442. case CE4100_SSP:
  1443. tmp = CE4100_SSCR1_RxTresh(RX_THRESH_CE4100_DFLT) |
  1444. CE4100_SSCR1_TxTresh(TX_THRESH_CE4100_DFLT);
  1445. pxa2xx_spi_write(drv_data, SSCR1, tmp);
  1446. tmp = SSCR0_SCR(2) | SSCR0_Motorola | SSCR0_DataSize(8);
  1447. pxa2xx_spi_write(drv_data, SSCR0, tmp);
  1448. break;
  1449. default:
  1450. tmp = SSCR1_RxTresh(RX_THRESH_DFLT) |
  1451. SSCR1_TxTresh(TX_THRESH_DFLT);
  1452. pxa2xx_spi_write(drv_data, SSCR1, tmp);
  1453. tmp = SSCR0_SCR(2) | SSCR0_Motorola | SSCR0_DataSize(8);
  1454. pxa2xx_spi_write(drv_data, SSCR0, tmp);
  1455. break;
  1456. }
  1457. if (!pxa25x_ssp_comp(drv_data))
  1458. pxa2xx_spi_write(drv_data, SSTO, 0);
  1459. if (!is_quark_x1000_ssp(drv_data))
  1460. pxa2xx_spi_write(drv_data, SSPSP, 0);
  1461. if (is_lpss_ssp(drv_data)) {
  1462. lpss_ssp_setup(drv_data);
  1463. config = lpss_get_config(drv_data);
  1464. if (config->reg_capabilities >= 0) {
  1465. tmp = __lpss_ssp_read_priv(drv_data,
  1466. config->reg_capabilities);
  1467. tmp &= LPSS_CAPS_CS_EN_MASK;
  1468. tmp >>= LPSS_CAPS_CS_EN_SHIFT;
  1469. platform_info->num_chipselect = ffz(tmp);
  1470. } else if (config->cs_num) {
  1471. platform_info->num_chipselect = config->cs_num;
  1472. }
  1473. }
  1474. master->num_chipselect = platform_info->num_chipselect;
  1475. count = gpiod_count(&pdev->dev, "cs");
  1476. if (count > 0) {
  1477. int i;
  1478. master->num_chipselect = max_t(int, count,
  1479. master->num_chipselect);
  1480. drv_data->cs_gpiods = devm_kcalloc(&pdev->dev,
  1481. master->num_chipselect, sizeof(struct gpio_desc *),
  1482. GFP_KERNEL);
  1483. if (!drv_data->cs_gpiods) {
  1484. status = -ENOMEM;
  1485. goto out_error_clock_enabled;
  1486. }
  1487. for (i = 0; i < master->num_chipselect; i++) {
  1488. struct gpio_desc *gpiod;
  1489. gpiod = devm_gpiod_get_index(dev, "cs", i,
  1490. GPIOD_OUT_HIGH);
  1491. if (IS_ERR(gpiod)) {
  1492. /* Means use native chip select */
  1493. if (PTR_ERR(gpiod) == -ENOENT)
  1494. continue;
  1495. status = (int)PTR_ERR(gpiod);
  1496. goto out_error_clock_enabled;
  1497. } else {
  1498. drv_data->cs_gpiods[i] = gpiod;
  1499. }
  1500. }
  1501. }
  1502. tasklet_init(&drv_data->pump_transfers, pump_transfers,
  1503. (unsigned long)drv_data);
  1504. pm_runtime_set_autosuspend_delay(&pdev->dev, 50);
  1505. pm_runtime_use_autosuspend(&pdev->dev);
  1506. pm_runtime_set_active(&pdev->dev);
  1507. pm_runtime_enable(&pdev->dev);
  1508. /* Register with the SPI framework */
  1509. platform_set_drvdata(pdev, drv_data);
  1510. status = devm_spi_register_master(&pdev->dev, master);
  1511. if (status != 0) {
  1512. dev_err(&pdev->dev, "problem registering spi master\n");
  1513. goto out_error_clock_enabled;
  1514. }
  1515. return status;
  1516. out_error_clock_enabled:
  1517. clk_disable_unprepare(ssp->clk);
  1518. pxa2xx_spi_dma_release(drv_data);
  1519. free_irq(ssp->irq, drv_data);
  1520. out_error_master_alloc:
  1521. spi_master_put(master);
  1522. pxa_ssp_free(ssp);
  1523. return status;
  1524. }
  1525. static int pxa2xx_spi_remove(struct platform_device *pdev)
  1526. {
  1527. struct driver_data *drv_data = platform_get_drvdata(pdev);
  1528. struct ssp_device *ssp;
  1529. if (!drv_data)
  1530. return 0;
  1531. ssp = drv_data->ssp;
  1532. pm_runtime_get_sync(&pdev->dev);
  1533. /* Disable the SSP at the peripheral and SOC level */
  1534. pxa2xx_spi_write(drv_data, SSCR0, 0);
  1535. clk_disable_unprepare(ssp->clk);
  1536. /* Release DMA */
  1537. if (drv_data->master_info->enable_dma)
  1538. pxa2xx_spi_dma_release(drv_data);
  1539. pm_runtime_put_noidle(&pdev->dev);
  1540. pm_runtime_disable(&pdev->dev);
  1541. /* Release IRQ */
  1542. free_irq(ssp->irq, drv_data);
  1543. /* Release SSP */
  1544. pxa_ssp_free(ssp);
  1545. return 0;
  1546. }
  1547. static void pxa2xx_spi_shutdown(struct platform_device *pdev)
  1548. {
  1549. int status = 0;
  1550. if ((status = pxa2xx_spi_remove(pdev)) != 0)
  1551. dev_err(&pdev->dev, "shutdown failed with %d\n", status);
  1552. }
  1553. #ifdef CONFIG_PM_SLEEP
  1554. static int pxa2xx_spi_suspend(struct device *dev)
  1555. {
  1556. struct driver_data *drv_data = dev_get_drvdata(dev);
  1557. struct ssp_device *ssp = drv_data->ssp;
  1558. int status;
  1559. status = spi_master_suspend(drv_data->master);
  1560. if (status != 0)
  1561. return status;
  1562. pxa2xx_spi_write(drv_data, SSCR0, 0);
  1563. if (!pm_runtime_suspended(dev))
  1564. clk_disable_unprepare(ssp->clk);
  1565. return 0;
  1566. }
  1567. static int pxa2xx_spi_resume(struct device *dev)
  1568. {
  1569. struct driver_data *drv_data = dev_get_drvdata(dev);
  1570. struct ssp_device *ssp = drv_data->ssp;
  1571. int status;
  1572. /* Enable the SSP clock */
  1573. if (!pm_runtime_suspended(dev))
  1574. clk_prepare_enable(ssp->clk);
  1575. /* Restore LPSS private register bits */
  1576. if (is_lpss_ssp(drv_data))
  1577. lpss_ssp_setup(drv_data);
  1578. /* Start the queue running */
  1579. status = spi_master_resume(drv_data->master);
  1580. if (status != 0) {
  1581. dev_err(dev, "problem starting queue (%d)\n", status);
  1582. return status;
  1583. }
  1584. return 0;
  1585. }
  1586. #endif
  1587. #ifdef CONFIG_PM
  1588. static int pxa2xx_spi_runtime_suspend(struct device *dev)
  1589. {
  1590. struct driver_data *drv_data = dev_get_drvdata(dev);
  1591. clk_disable_unprepare(drv_data->ssp->clk);
  1592. return 0;
  1593. }
  1594. static int pxa2xx_spi_runtime_resume(struct device *dev)
  1595. {
  1596. struct driver_data *drv_data = dev_get_drvdata(dev);
  1597. clk_prepare_enable(drv_data->ssp->clk);
  1598. return 0;
  1599. }
  1600. #endif
  1601. static const struct dev_pm_ops pxa2xx_spi_pm_ops = {
  1602. SET_SYSTEM_SLEEP_PM_OPS(pxa2xx_spi_suspend, pxa2xx_spi_resume)
  1603. SET_RUNTIME_PM_OPS(pxa2xx_spi_runtime_suspend,
  1604. pxa2xx_spi_runtime_resume, NULL)
  1605. };
  1606. static struct platform_driver driver = {
  1607. .driver = {
  1608. .name = "pxa2xx-spi",
  1609. .pm = &pxa2xx_spi_pm_ops,
  1610. .acpi_match_table = ACPI_PTR(pxa2xx_spi_acpi_match),
  1611. },
  1612. .probe = pxa2xx_spi_probe,
  1613. .remove = pxa2xx_spi_remove,
  1614. .shutdown = pxa2xx_spi_shutdown,
  1615. };
  1616. static int __init pxa2xx_spi_init(void)
  1617. {
  1618. return platform_driver_register(&driver);
  1619. }
  1620. subsys_initcall(pxa2xx_spi_init);
  1621. static void __exit pxa2xx_spi_exit(void)
  1622. {
  1623. platform_driver_unregister(&driver);
  1624. }
  1625. module_exit(pxa2xx_spi_exit);