atmel-ebi.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761
  1. /*
  2. * EBI driver for Atmel chips
  3. * inspired by the fsl weim bus driver
  4. *
  5. * Copyright (C) 2013 Jean-Jacques Hiblot <jjhiblot@traphandler.com>
  6. *
  7. * This file is licensed under the terms of the GNU General Public
  8. * License version 2. This program is licensed "as is" without any
  9. * warranty of any kind, whether express or implied.
  10. */
  11. #include <linux/clk.h>
  12. #include <linux/io.h>
  13. #include <linux/mfd/syscon.h>
  14. #include <linux/mfd/syscon/atmel-matrix.h>
  15. #include <linux/mfd/syscon/atmel-smc.h>
  16. #include <linux/init.h>
  17. #include <linux/of_device.h>
  18. #include <linux/regmap.h>
  19. struct at91sam9_smc_timings {
  20. u32 ncs_rd_setup_ns;
  21. u32 nrd_setup_ns;
  22. u32 ncs_wr_setup_ns;
  23. u32 nwe_setup_ns;
  24. u32 ncs_rd_pulse_ns;
  25. u32 nrd_pulse_ns;
  26. u32 ncs_wr_pulse_ns;
  27. u32 nwe_pulse_ns;
  28. u32 nrd_cycle_ns;
  29. u32 nwe_cycle_ns;
  30. u32 tdf_ns;
  31. };
  32. struct at91sam9_smc_generic_fields {
  33. struct regmap_field *setup;
  34. struct regmap_field *pulse;
  35. struct regmap_field *cycle;
  36. struct regmap_field *mode;
  37. };
  38. struct at91sam9_ebi_dev_config {
  39. struct at91sam9_smc_timings timings;
  40. u32 mode;
  41. };
  42. struct at91_ebi_dev_config {
  43. int cs;
  44. union {
  45. struct at91sam9_ebi_dev_config sam9;
  46. };
  47. };
  48. struct at91_ebi;
  49. struct at91_ebi_dev {
  50. struct list_head node;
  51. struct at91_ebi *ebi;
  52. u32 mode;
  53. int numcs;
  54. struct at91_ebi_dev_config configs[];
  55. };
  56. struct at91_ebi_caps {
  57. unsigned int available_cs;
  58. const struct reg_field *ebi_csa;
  59. void (*get_config)(struct at91_ebi_dev *ebid,
  60. struct at91_ebi_dev_config *conf);
  61. int (*xlate_config)(struct at91_ebi_dev *ebid,
  62. struct device_node *configs_np,
  63. struct at91_ebi_dev_config *conf);
  64. int (*apply_config)(struct at91_ebi_dev *ebid,
  65. struct at91_ebi_dev_config *conf);
  66. int (*init)(struct at91_ebi *ebi);
  67. };
  68. struct at91_ebi {
  69. struct clk *clk;
  70. struct regmap *smc;
  71. struct regmap *matrix;
  72. struct regmap_field *ebi_csa;
  73. struct device *dev;
  74. const struct at91_ebi_caps *caps;
  75. struct list_head devs;
  76. union {
  77. struct at91sam9_smc_generic_fields sam9;
  78. };
  79. };
  80. static void at91sam9_ebi_get_config(struct at91_ebi_dev *ebid,
  81. struct at91_ebi_dev_config *conf)
  82. {
  83. struct at91sam9_smc_generic_fields *fields = &ebid->ebi->sam9;
  84. unsigned int clk_rate = clk_get_rate(ebid->ebi->clk);
  85. struct at91sam9_ebi_dev_config *config = &conf->sam9;
  86. struct at91sam9_smc_timings *timings = &config->timings;
  87. unsigned int val;
  88. regmap_fields_read(fields->mode, conf->cs, &val);
  89. config->mode = val & ~AT91_SMC_TDF;
  90. val = (val & AT91_SMC_TDF) >> 16;
  91. timings->tdf_ns = clk_rate * val;
  92. regmap_fields_read(fields->setup, conf->cs, &val);
  93. timings->ncs_rd_setup_ns = (val >> 24) & 0x1f;
  94. timings->ncs_rd_setup_ns += ((val >> 29) & 0x1) * 128;
  95. timings->ncs_rd_setup_ns *= clk_rate;
  96. timings->nrd_setup_ns = (val >> 16) & 0x1f;
  97. timings->nrd_setup_ns += ((val >> 21) & 0x1) * 128;
  98. timings->nrd_setup_ns *= clk_rate;
  99. timings->ncs_wr_setup_ns = (val >> 8) & 0x1f;
  100. timings->ncs_wr_setup_ns += ((val >> 13) & 0x1) * 128;
  101. timings->ncs_wr_setup_ns *= clk_rate;
  102. timings->nwe_setup_ns = val & 0x1f;
  103. timings->nwe_setup_ns += ((val >> 5) & 0x1) * 128;
  104. timings->nwe_setup_ns *= clk_rate;
  105. regmap_fields_read(fields->pulse, conf->cs, &val);
  106. timings->ncs_rd_pulse_ns = (val >> 24) & 0x3f;
  107. timings->ncs_rd_pulse_ns += ((val >> 30) & 0x1) * 256;
  108. timings->ncs_rd_pulse_ns *= clk_rate;
  109. timings->nrd_pulse_ns = (val >> 16) & 0x3f;
  110. timings->nrd_pulse_ns += ((val >> 22) & 0x1) * 256;
  111. timings->nrd_pulse_ns *= clk_rate;
  112. timings->ncs_wr_pulse_ns = (val >> 8) & 0x3f;
  113. timings->ncs_wr_pulse_ns += ((val >> 14) & 0x1) * 256;
  114. timings->ncs_wr_pulse_ns *= clk_rate;
  115. timings->nwe_pulse_ns = val & 0x3f;
  116. timings->nwe_pulse_ns += ((val >> 6) & 0x1) * 256;
  117. timings->nwe_pulse_ns *= clk_rate;
  118. regmap_fields_read(fields->cycle, conf->cs, &val);
  119. timings->nrd_cycle_ns = (val >> 16) & 0x7f;
  120. timings->nrd_cycle_ns += ((val >> 23) & 0x3) * 256;
  121. timings->nrd_cycle_ns *= clk_rate;
  122. timings->nwe_cycle_ns = val & 0x7f;
  123. timings->nwe_cycle_ns += ((val >> 7) & 0x3) * 256;
  124. timings->nwe_cycle_ns *= clk_rate;
  125. }
  126. static int at91_xlate_timing(struct device_node *np, const char *prop,
  127. u32 *val, bool *required)
  128. {
  129. if (!of_property_read_u32(np, prop, val)) {
  130. *required = true;
  131. return 0;
  132. }
  133. if (*required)
  134. return -EINVAL;
  135. return 0;
  136. }
  137. static int at91sam9_smc_xslate_timings(struct at91_ebi_dev *ebid,
  138. struct device_node *np,
  139. struct at91sam9_smc_timings *timings,
  140. bool *required)
  141. {
  142. int ret;
  143. ret = at91_xlate_timing(np, "atmel,smc-ncs-rd-setup-ns",
  144. &timings->ncs_rd_setup_ns, required);
  145. if (ret)
  146. goto out;
  147. ret = at91_xlate_timing(np, "atmel,smc-nrd-setup-ns",
  148. &timings->nrd_setup_ns, required);
  149. if (ret)
  150. goto out;
  151. ret = at91_xlate_timing(np, "atmel,smc-ncs-wr-setup-ns",
  152. &timings->ncs_wr_setup_ns, required);
  153. if (ret)
  154. goto out;
  155. ret = at91_xlate_timing(np, "atmel,smc-nwe-setup-ns",
  156. &timings->nwe_setup_ns, required);
  157. if (ret)
  158. goto out;
  159. ret = at91_xlate_timing(np, "atmel,smc-ncs-rd-pulse-ns",
  160. &timings->ncs_rd_pulse_ns, required);
  161. if (ret)
  162. goto out;
  163. ret = at91_xlate_timing(np, "atmel,smc-nrd-pulse-ns",
  164. &timings->nrd_pulse_ns, required);
  165. if (ret)
  166. goto out;
  167. ret = at91_xlate_timing(np, "atmel,smc-ncs-wr-pulse-ns",
  168. &timings->ncs_wr_pulse_ns, required);
  169. if (ret)
  170. goto out;
  171. ret = at91_xlate_timing(np, "atmel,smc-nwe-pulse-ns",
  172. &timings->nwe_pulse_ns, required);
  173. if (ret)
  174. goto out;
  175. ret = at91_xlate_timing(np, "atmel,smc-nwe-cycle-ns",
  176. &timings->nwe_cycle_ns, required);
  177. if (ret)
  178. goto out;
  179. ret = at91_xlate_timing(np, "atmel,smc-nrd-cycle-ns",
  180. &timings->nrd_cycle_ns, required);
  181. if (ret)
  182. goto out;
  183. ret = at91_xlate_timing(np, "atmel,smc-tdf-ns",
  184. &timings->tdf_ns, required);
  185. out:
  186. if (ret)
  187. dev_err(ebid->ebi->dev,
  188. "missing or invalid timings definition in %s",
  189. np->full_name);
  190. return ret;
  191. }
  192. static int at91sam9_ebi_xslate_config(struct at91_ebi_dev *ebid,
  193. struct device_node *np,
  194. struct at91_ebi_dev_config *conf)
  195. {
  196. struct at91sam9_ebi_dev_config *config = &conf->sam9;
  197. bool required = false;
  198. const char *tmp_str;
  199. u32 tmp;
  200. int ret;
  201. ret = of_property_read_u32(np, "atmel,smc-bus-width", &tmp);
  202. if (!ret) {
  203. switch (tmp) {
  204. case 8:
  205. config->mode |= AT91_SMC_DBW_8;
  206. break;
  207. case 16:
  208. config->mode |= AT91_SMC_DBW_16;
  209. break;
  210. case 32:
  211. config->mode |= AT91_SMC_DBW_32;
  212. break;
  213. default:
  214. return -EINVAL;
  215. }
  216. required = true;
  217. }
  218. if (of_property_read_bool(np, "atmel,smc-tdf-optimized")) {
  219. config->mode |= AT91_SMC_TDFMODE_OPTIMIZED;
  220. required = true;
  221. }
  222. tmp_str = NULL;
  223. of_property_read_string(np, "atmel,smc-byte-access-type", &tmp_str);
  224. if (tmp_str && !strcmp(tmp_str, "write")) {
  225. config->mode |= AT91_SMC_BAT_WRITE;
  226. required = true;
  227. }
  228. tmp_str = NULL;
  229. of_property_read_string(np, "atmel,smc-read-mode", &tmp_str);
  230. if (tmp_str && !strcmp(tmp_str, "nrd")) {
  231. config->mode |= AT91_SMC_READMODE_NRD;
  232. required = true;
  233. }
  234. tmp_str = NULL;
  235. of_property_read_string(np, "atmel,smc-write-mode", &tmp_str);
  236. if (tmp_str && !strcmp(tmp_str, "nwe")) {
  237. config->mode |= AT91_SMC_WRITEMODE_NWE;
  238. required = true;
  239. }
  240. tmp_str = NULL;
  241. of_property_read_string(np, "atmel,smc-exnw-mode", &tmp_str);
  242. if (tmp_str) {
  243. if (!strcmp(tmp_str, "frozen"))
  244. config->mode |= AT91_SMC_EXNWMODE_FROZEN;
  245. else if (!strcmp(tmp_str, "ready"))
  246. config->mode |= AT91_SMC_EXNWMODE_READY;
  247. else if (strcmp(tmp_str, "disabled"))
  248. return -EINVAL;
  249. required = true;
  250. }
  251. ret = of_property_read_u32(np, "atmel,smc-page-mode", &tmp);
  252. if (!ret) {
  253. switch (tmp) {
  254. case 4:
  255. config->mode |= AT91_SMC_PS_4;
  256. break;
  257. case 8:
  258. config->mode |= AT91_SMC_PS_8;
  259. break;
  260. case 16:
  261. config->mode |= AT91_SMC_PS_16;
  262. break;
  263. case 32:
  264. config->mode |= AT91_SMC_PS_32;
  265. break;
  266. default:
  267. return -EINVAL;
  268. }
  269. config->mode |= AT91_SMC_PMEN;
  270. required = true;
  271. }
  272. ret = at91sam9_smc_xslate_timings(ebid, np, &config->timings,
  273. &required);
  274. if (ret)
  275. return ret;
  276. return required;
  277. }
  278. static int at91sam9_ebi_apply_config(struct at91_ebi_dev *ebid,
  279. struct at91_ebi_dev_config *conf)
  280. {
  281. unsigned int clk_rate = clk_get_rate(ebid->ebi->clk);
  282. struct at91sam9_ebi_dev_config *config = &conf->sam9;
  283. struct at91sam9_smc_timings *timings = &config->timings;
  284. struct at91sam9_smc_generic_fields *fields = &ebid->ebi->sam9;
  285. u32 coded_val;
  286. u32 val;
  287. coded_val = at91sam9_smc_setup_ns_to_cycles(clk_rate,
  288. timings->ncs_rd_setup_ns);
  289. val = AT91SAM9_SMC_NCS_NRDSETUP(coded_val);
  290. coded_val = at91sam9_smc_setup_ns_to_cycles(clk_rate,
  291. timings->nrd_setup_ns);
  292. val |= AT91SAM9_SMC_NRDSETUP(coded_val);
  293. coded_val = at91sam9_smc_setup_ns_to_cycles(clk_rate,
  294. timings->ncs_wr_setup_ns);
  295. val |= AT91SAM9_SMC_NCS_WRSETUP(coded_val);
  296. coded_val = at91sam9_smc_setup_ns_to_cycles(clk_rate,
  297. timings->nwe_setup_ns);
  298. val |= AT91SAM9_SMC_NWESETUP(coded_val);
  299. regmap_fields_write(fields->setup, conf->cs, val);
  300. coded_val = at91sam9_smc_pulse_ns_to_cycles(clk_rate,
  301. timings->ncs_rd_pulse_ns);
  302. val = AT91SAM9_SMC_NCS_NRDPULSE(coded_val);
  303. coded_val = at91sam9_smc_pulse_ns_to_cycles(clk_rate,
  304. timings->nrd_pulse_ns);
  305. val |= AT91SAM9_SMC_NRDPULSE(coded_val);
  306. coded_val = at91sam9_smc_pulse_ns_to_cycles(clk_rate,
  307. timings->ncs_wr_pulse_ns);
  308. val |= AT91SAM9_SMC_NCS_WRPULSE(coded_val);
  309. coded_val = at91sam9_smc_pulse_ns_to_cycles(clk_rate,
  310. timings->nwe_pulse_ns);
  311. val |= AT91SAM9_SMC_NWEPULSE(coded_val);
  312. regmap_fields_write(fields->pulse, conf->cs, val);
  313. coded_val = at91sam9_smc_cycle_ns_to_cycles(clk_rate,
  314. timings->nrd_cycle_ns);
  315. val = AT91SAM9_SMC_NRDCYCLE(coded_val);
  316. coded_val = at91sam9_smc_cycle_ns_to_cycles(clk_rate,
  317. timings->nwe_cycle_ns);
  318. val |= AT91SAM9_SMC_NWECYCLE(coded_val);
  319. regmap_fields_write(fields->cycle, conf->cs, val);
  320. val = DIV_ROUND_UP(timings->tdf_ns, clk_rate);
  321. if (val > AT91_SMC_TDF_MAX)
  322. val = AT91_SMC_TDF_MAX;
  323. regmap_fields_write(fields->mode, conf->cs,
  324. config->mode | AT91_SMC_TDF_(val));
  325. return 0;
  326. }
  327. static int at91sam9_ebi_init(struct at91_ebi *ebi)
  328. {
  329. struct at91sam9_smc_generic_fields *fields = &ebi->sam9;
  330. struct reg_field field = REG_FIELD(0, 0, 31);
  331. field.id_size = fls(ebi->caps->available_cs);
  332. field.id_offset = AT91SAM9_SMC_GENERIC_BLK_SZ;
  333. field.reg = AT91SAM9_SMC_SETUP(AT91SAM9_SMC_GENERIC);
  334. fields->setup = devm_regmap_field_alloc(ebi->dev, ebi->smc, field);
  335. if (IS_ERR(fields->setup))
  336. return PTR_ERR(fields->setup);
  337. field.reg = AT91SAM9_SMC_PULSE(AT91SAM9_SMC_GENERIC);
  338. fields->pulse = devm_regmap_field_alloc(ebi->dev, ebi->smc, field);
  339. if (IS_ERR(fields->pulse))
  340. return PTR_ERR(fields->pulse);
  341. field.reg = AT91SAM9_SMC_CYCLE(AT91SAM9_SMC_GENERIC);
  342. fields->cycle = devm_regmap_field_alloc(ebi->dev, ebi->smc, field);
  343. if (IS_ERR(fields->cycle))
  344. return PTR_ERR(fields->cycle);
  345. field.reg = AT91SAM9_SMC_MODE(AT91SAM9_SMC_GENERIC);
  346. fields->mode = devm_regmap_field_alloc(ebi->dev, ebi->smc, field);
  347. return PTR_ERR_OR_ZERO(fields->mode);
  348. }
  349. static int sama5d3_ebi_init(struct at91_ebi *ebi)
  350. {
  351. struct at91sam9_smc_generic_fields *fields = &ebi->sam9;
  352. struct reg_field field = REG_FIELD(0, 0, 31);
  353. field.id_size = fls(ebi->caps->available_cs);
  354. field.id_offset = SAMA5_SMC_GENERIC_BLK_SZ;
  355. field.reg = AT91SAM9_SMC_SETUP(SAMA5_SMC_GENERIC);
  356. fields->setup = devm_regmap_field_alloc(ebi->dev, ebi->smc, field);
  357. if (IS_ERR(fields->setup))
  358. return PTR_ERR(fields->setup);
  359. field.reg = AT91SAM9_SMC_PULSE(SAMA5_SMC_GENERIC);
  360. fields->pulse = devm_regmap_field_alloc(ebi->dev, ebi->smc, field);
  361. if (IS_ERR(fields->pulse))
  362. return PTR_ERR(fields->pulse);
  363. field.reg = AT91SAM9_SMC_CYCLE(SAMA5_SMC_GENERIC);
  364. fields->cycle = devm_regmap_field_alloc(ebi->dev, ebi->smc, field);
  365. if (IS_ERR(fields->cycle))
  366. return PTR_ERR(fields->cycle);
  367. field.reg = SAMA5_SMC_MODE(SAMA5_SMC_GENERIC);
  368. fields->mode = devm_regmap_field_alloc(ebi->dev, ebi->smc, field);
  369. return PTR_ERR_OR_ZERO(fields->mode);
  370. }
  371. static int at91_ebi_dev_setup(struct at91_ebi *ebi, struct device_node *np,
  372. int reg_cells)
  373. {
  374. const struct at91_ebi_caps *caps = ebi->caps;
  375. struct at91_ebi_dev_config conf = { };
  376. struct device *dev = ebi->dev;
  377. struct at91_ebi_dev *ebid;
  378. int ret, numcs = 0, i;
  379. bool apply = false;
  380. numcs = of_property_count_elems_of_size(np, "reg",
  381. reg_cells * sizeof(u32));
  382. if (numcs <= 0) {
  383. dev_err(dev, "invalid reg property in %s\n", np->full_name);
  384. return -EINVAL;
  385. }
  386. ebid = devm_kzalloc(ebi->dev,
  387. sizeof(*ebid) + (numcs * sizeof(*ebid->configs)),
  388. GFP_KERNEL);
  389. if (!ebid)
  390. return -ENOMEM;
  391. ebid->ebi = ebi;
  392. ret = caps->xlate_config(ebid, np, &conf);
  393. if (ret < 0)
  394. return ret;
  395. else if (ret)
  396. apply = true;
  397. for (i = 0; i < numcs; i++) {
  398. u32 cs;
  399. ret = of_property_read_u32_index(np, "reg", i * reg_cells,
  400. &cs);
  401. if (ret)
  402. return ret;
  403. if (cs > AT91_MATRIX_EBI_NUM_CS ||
  404. !(ebi->caps->available_cs & BIT(cs))) {
  405. dev_err(dev, "invalid reg property in %s\n",
  406. np->full_name);
  407. return -EINVAL;
  408. }
  409. ebid->configs[i].cs = cs;
  410. if (apply) {
  411. conf.cs = cs;
  412. ret = caps->apply_config(ebid, &conf);
  413. if (ret)
  414. return ret;
  415. }
  416. caps->get_config(ebid, &ebid->configs[i]);
  417. /*
  418. * Attach the EBI device to the generic SMC logic if at least
  419. * one "atmel,smc-" property is present.
  420. */
  421. if (ebi->ebi_csa && ret)
  422. regmap_field_update_bits(ebi->ebi_csa,
  423. BIT(cs), 0);
  424. }
  425. list_add_tail(&ebid->node, &ebi->devs);
  426. return 0;
  427. }
  428. static const struct reg_field at91sam9260_ebi_csa =
  429. REG_FIELD(AT91SAM9260_MATRIX_EBICSA, 0,
  430. AT91_MATRIX_EBI_NUM_CS - 1);
  431. static const struct at91_ebi_caps at91sam9260_ebi_caps = {
  432. .available_cs = 0xff,
  433. .ebi_csa = &at91sam9260_ebi_csa,
  434. .get_config = at91sam9_ebi_get_config,
  435. .xlate_config = at91sam9_ebi_xslate_config,
  436. .apply_config = at91sam9_ebi_apply_config,
  437. .init = at91sam9_ebi_init,
  438. };
  439. static const struct reg_field at91sam9261_ebi_csa =
  440. REG_FIELD(AT91SAM9261_MATRIX_EBICSA, 0,
  441. AT91_MATRIX_EBI_NUM_CS - 1);
  442. static const struct at91_ebi_caps at91sam9261_ebi_caps = {
  443. .available_cs = 0xff,
  444. .ebi_csa = &at91sam9261_ebi_csa,
  445. .get_config = at91sam9_ebi_get_config,
  446. .xlate_config = at91sam9_ebi_xslate_config,
  447. .apply_config = at91sam9_ebi_apply_config,
  448. .init = at91sam9_ebi_init,
  449. };
  450. static const struct reg_field at91sam9263_ebi0_csa =
  451. REG_FIELD(AT91SAM9263_MATRIX_EBI0CSA, 0,
  452. AT91_MATRIX_EBI_NUM_CS - 1);
  453. static const struct at91_ebi_caps at91sam9263_ebi0_caps = {
  454. .available_cs = 0x3f,
  455. .ebi_csa = &at91sam9263_ebi0_csa,
  456. .get_config = at91sam9_ebi_get_config,
  457. .xlate_config = at91sam9_ebi_xslate_config,
  458. .apply_config = at91sam9_ebi_apply_config,
  459. .init = at91sam9_ebi_init,
  460. };
  461. static const struct reg_field at91sam9263_ebi1_csa =
  462. REG_FIELD(AT91SAM9263_MATRIX_EBI1CSA, 0,
  463. AT91_MATRIX_EBI_NUM_CS - 1);
  464. static const struct at91_ebi_caps at91sam9263_ebi1_caps = {
  465. .available_cs = 0x7,
  466. .ebi_csa = &at91sam9263_ebi1_csa,
  467. .get_config = at91sam9_ebi_get_config,
  468. .xlate_config = at91sam9_ebi_xslate_config,
  469. .apply_config = at91sam9_ebi_apply_config,
  470. .init = at91sam9_ebi_init,
  471. };
  472. static const struct reg_field at91sam9rl_ebi_csa =
  473. REG_FIELD(AT91SAM9RL_MATRIX_EBICSA, 0,
  474. AT91_MATRIX_EBI_NUM_CS - 1);
  475. static const struct at91_ebi_caps at91sam9rl_ebi_caps = {
  476. .available_cs = 0x3f,
  477. .ebi_csa = &at91sam9rl_ebi_csa,
  478. .get_config = at91sam9_ebi_get_config,
  479. .xlate_config = at91sam9_ebi_xslate_config,
  480. .apply_config = at91sam9_ebi_apply_config,
  481. .init = at91sam9_ebi_init,
  482. };
  483. static const struct reg_field at91sam9g45_ebi_csa =
  484. REG_FIELD(AT91SAM9G45_MATRIX_EBICSA, 0,
  485. AT91_MATRIX_EBI_NUM_CS - 1);
  486. static const struct at91_ebi_caps at91sam9g45_ebi_caps = {
  487. .available_cs = 0x3f,
  488. .ebi_csa = &at91sam9g45_ebi_csa,
  489. .get_config = at91sam9_ebi_get_config,
  490. .xlate_config = at91sam9_ebi_xslate_config,
  491. .apply_config = at91sam9_ebi_apply_config,
  492. .init = at91sam9_ebi_init,
  493. };
  494. static const struct at91_ebi_caps at91sam9x5_ebi_caps = {
  495. .available_cs = 0x3f,
  496. .ebi_csa = &at91sam9263_ebi0_csa,
  497. .get_config = at91sam9_ebi_get_config,
  498. .xlate_config = at91sam9_ebi_xslate_config,
  499. .apply_config = at91sam9_ebi_apply_config,
  500. .init = at91sam9_ebi_init,
  501. };
  502. static const struct at91_ebi_caps sama5d3_ebi_caps = {
  503. .available_cs = 0xf,
  504. .get_config = at91sam9_ebi_get_config,
  505. .xlate_config = at91sam9_ebi_xslate_config,
  506. .apply_config = at91sam9_ebi_apply_config,
  507. .init = sama5d3_ebi_init,
  508. };
  509. static const struct of_device_id at91_ebi_id_table[] = {
  510. {
  511. .compatible = "atmel,at91sam9260-ebi",
  512. .data = &at91sam9260_ebi_caps,
  513. },
  514. {
  515. .compatible = "atmel,at91sam9261-ebi",
  516. .data = &at91sam9261_ebi_caps,
  517. },
  518. {
  519. .compatible = "atmel,at91sam9263-ebi0",
  520. .data = &at91sam9263_ebi0_caps,
  521. },
  522. {
  523. .compatible = "atmel,at91sam9263-ebi1",
  524. .data = &at91sam9263_ebi1_caps,
  525. },
  526. {
  527. .compatible = "atmel,at91sam9rl-ebi",
  528. .data = &at91sam9rl_ebi_caps,
  529. },
  530. {
  531. .compatible = "atmel,at91sam9g45-ebi",
  532. .data = &at91sam9g45_ebi_caps,
  533. },
  534. {
  535. .compatible = "atmel,at91sam9x5-ebi",
  536. .data = &at91sam9x5_ebi_caps,
  537. },
  538. {
  539. .compatible = "atmel,sama5d3-ebi",
  540. .data = &sama5d3_ebi_caps,
  541. },
  542. { /* sentinel */ }
  543. };
  544. static int at91_ebi_dev_disable(struct at91_ebi *ebi, struct device_node *np)
  545. {
  546. struct device *dev = ebi->dev;
  547. struct property *newprop;
  548. newprop = devm_kzalloc(dev, sizeof(*newprop), GFP_KERNEL);
  549. if (!newprop)
  550. return -ENOMEM;
  551. newprop->name = devm_kstrdup(dev, "status", GFP_KERNEL);
  552. if (!newprop->name)
  553. return -ENOMEM;
  554. newprop->value = devm_kstrdup(dev, "disabled", GFP_KERNEL);
  555. if (!newprop->value)
  556. return -ENOMEM;
  557. newprop->length = sizeof("disabled");
  558. return of_update_property(np, newprop);
  559. }
  560. static int at91_ebi_probe(struct platform_device *pdev)
  561. {
  562. struct device *dev = &pdev->dev;
  563. struct device_node *child, *np = dev->of_node;
  564. const struct of_device_id *match;
  565. struct at91_ebi *ebi;
  566. int ret, reg_cells;
  567. struct clk *clk;
  568. u32 val;
  569. match = of_match_device(at91_ebi_id_table, dev);
  570. if (!match || !match->data)
  571. return -EINVAL;
  572. ebi = devm_kzalloc(dev, sizeof(*ebi), GFP_KERNEL);
  573. if (!ebi)
  574. return -ENOMEM;
  575. INIT_LIST_HEAD(&ebi->devs);
  576. ebi->caps = match->data;
  577. ebi->dev = dev;
  578. clk = devm_clk_get(dev, NULL);
  579. if (IS_ERR(clk))
  580. return PTR_ERR(clk);
  581. ebi->clk = clk;
  582. ebi->smc = syscon_regmap_lookup_by_phandle(np, "atmel,smc");
  583. if (IS_ERR(ebi->smc))
  584. return PTR_ERR(ebi->smc);
  585. /*
  586. * The sama5d3 does not provide an EBICSA register and thus does need
  587. * to access the matrix registers.
  588. */
  589. if (ebi->caps->ebi_csa) {
  590. ebi->matrix =
  591. syscon_regmap_lookup_by_phandle(np, "atmel,matrix");
  592. if (IS_ERR(ebi->matrix))
  593. return PTR_ERR(ebi->matrix);
  594. ebi->ebi_csa = regmap_field_alloc(ebi->matrix,
  595. *ebi->caps->ebi_csa);
  596. if (IS_ERR(ebi->ebi_csa))
  597. return PTR_ERR(ebi->ebi_csa);
  598. }
  599. ret = ebi->caps->init(ebi);
  600. if (ret)
  601. return ret;
  602. ret = of_property_read_u32(np, "#address-cells", &val);
  603. if (ret) {
  604. dev_err(dev, "missing #address-cells property\n");
  605. return ret;
  606. }
  607. reg_cells = val;
  608. ret = of_property_read_u32(np, "#size-cells", &val);
  609. if (ret) {
  610. dev_err(dev, "missing #address-cells property\n");
  611. return ret;
  612. }
  613. reg_cells += val;
  614. for_each_available_child_of_node(np, child) {
  615. if (!of_find_property(child, "reg", NULL))
  616. continue;
  617. ret = at91_ebi_dev_setup(ebi, child, reg_cells);
  618. if (ret) {
  619. dev_err(dev, "failed to configure EBI bus for %s, disabling the device",
  620. child->full_name);
  621. ret = at91_ebi_dev_disable(ebi, child);
  622. if (ret)
  623. return ret;
  624. }
  625. }
  626. return of_platform_populate(np, NULL, NULL, dev);
  627. }
  628. static struct platform_driver at91_ebi_driver = {
  629. .driver = {
  630. .name = "atmel-ebi",
  631. .of_match_table = at91_ebi_id_table,
  632. },
  633. };
  634. builtin_platform_driver_probe(at91_ebi_driver, at91_ebi_probe);