dscr.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  1. /*
  2. * Device State Control Registers driver
  3. *
  4. * Copyright (C) 2011 Texas Instruments Incorporated
  5. * Author: Mark Salter <msalter@redhat.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. /*
  12. * The Device State Control Registers (DSCR) provide SoC level control over
  13. * a number of peripherals. Details vary considerably among the various SoC
  14. * parts. In general, the DSCR block will provide one or more configuration
  15. * registers often protected by a lock register. One or more key values must
  16. * be written to a lock register in order to unlock the configuration register.
  17. * The configuration register may be used to enable (and disable in some
  18. * cases) SoC pin drivers, peripheral clock sources (internal or pin), etc.
  19. * In some cases, a configuration register is write once or the individual
  20. * bits are write once. That is, you may be able to enable a device, but
  21. * will not be able to disable it.
  22. *
  23. * In addition to device configuration, the DSCR block may provide registers
  24. * which are used to reset SoC peripherals, provide device ID information,
  25. * provide MAC addresses, and other miscellaneous functions.
  26. */
  27. #include <linux/of.h>
  28. #include <linux/of_address.h>
  29. #include <linux/of_platform.h>
  30. #include <linux/module.h>
  31. #include <linux/io.h>
  32. #include <linux/delay.h>
  33. #include <asm/soc.h>
  34. #include <asm/dscr.h>
  35. #define MAX_DEVSTATE_IDS 32
  36. #define MAX_DEVCTL_REGS 8
  37. #define MAX_DEVSTAT_REGS 8
  38. #define MAX_LOCKED_REGS 4
  39. #define MAX_SOC_EMACS 2
  40. struct rmii_reset_reg {
  41. u32 reg;
  42. u32 mask;
  43. };
  44. /*
  45. * Some registerd may be locked. In order to write to these
  46. * registers, the key value must first be written to the lockreg.
  47. */
  48. struct locked_reg {
  49. u32 reg; /* offset from base */
  50. u32 lockreg; /* offset from base */
  51. u32 key; /* unlock key */
  52. };
  53. /*
  54. * This describes a contiguous area of like control bits used to enable/disable
  55. * SoC devices. Each controllable device is given an ID which is used by the
  56. * individual device drivers to control the device state. These IDs start at
  57. * zero and are assigned sequentially to the control bitfield ranges described
  58. * by this structure.
  59. */
  60. struct devstate_ctl_reg {
  61. u32 reg; /* register holding the control bits */
  62. u8 start_id; /* start id of this range */
  63. u8 num_ids; /* number of devices in this range */
  64. u8 enable_only; /* bits are write-once to enable only */
  65. u8 enable; /* value used to enable device */
  66. u8 disable; /* value used to disable device */
  67. u8 shift; /* starting (rightmost) bit in range */
  68. u8 nbits; /* number of bits per device */
  69. };
  70. /*
  71. * This describes a region of status bits indicating the state of
  72. * various devices. This is used internally to wait for status
  73. * change completion when enabling/disabling a device. Status is
  74. * optional and not all device controls will have a corresponding
  75. * status.
  76. */
  77. struct devstate_stat_reg {
  78. u32 reg; /* register holding the status bits */
  79. u8 start_id; /* start id of this range */
  80. u8 num_ids; /* number of devices in this range */
  81. u8 enable; /* value indicating enabled state */
  82. u8 disable; /* value indicating disabled state */
  83. u8 shift; /* starting (rightmost) bit in range */
  84. u8 nbits; /* number of bits per device */
  85. };
  86. struct devstate_info {
  87. struct devstate_ctl_reg *ctl;
  88. struct devstate_stat_reg *stat;
  89. };
  90. /* These are callbacks to SOC-specific code. */
  91. struct dscr_ops {
  92. void (*init)(struct device_node *node);
  93. };
  94. struct dscr_regs {
  95. spinlock_t lock;
  96. void __iomem *base;
  97. u32 kick_reg[2];
  98. u32 kick_key[2];
  99. struct locked_reg locked[MAX_LOCKED_REGS];
  100. struct devstate_info devstate_info[MAX_DEVSTATE_IDS];
  101. struct rmii_reset_reg rmii_resets[MAX_SOC_EMACS];
  102. struct devstate_ctl_reg devctl[MAX_DEVCTL_REGS];
  103. struct devstate_stat_reg devstat[MAX_DEVSTAT_REGS];
  104. };
  105. static struct dscr_regs dscr;
  106. static struct locked_reg *find_locked_reg(u32 reg)
  107. {
  108. int i;
  109. for (i = 0; i < MAX_LOCKED_REGS; i++)
  110. if (dscr.locked[i].key && reg == dscr.locked[i].reg)
  111. return &dscr.locked[i];
  112. return NULL;
  113. }
  114. /*
  115. * Write to a register with one lock
  116. */
  117. static void dscr_write_locked1(u32 reg, u32 val,
  118. u32 lock, u32 key)
  119. {
  120. void __iomem *reg_addr = dscr.base + reg;
  121. void __iomem *lock_addr = dscr.base + lock;
  122. /*
  123. * For some registers, the lock is relocked after a short number
  124. * of cycles. We have to put the lock write and register write in
  125. * the same fetch packet to meet this timing. The .align ensures
  126. * the two stw instructions are in the same fetch packet.
  127. */
  128. asm volatile ("b .s2 0f\n"
  129. "nop 5\n"
  130. " .align 5\n"
  131. "0:\n"
  132. "stw .D1T2 %3,*%2\n"
  133. "stw .D1T2 %1,*%0\n"
  134. :
  135. : "a"(reg_addr), "b"(val), "a"(lock_addr), "b"(key)
  136. );
  137. /* in case the hw doesn't reset the lock */
  138. soc_writel(0, lock_addr);
  139. }
  140. /*
  141. * Write to a register protected by two lock registers
  142. */
  143. static void dscr_write_locked2(u32 reg, u32 val,
  144. u32 lock0, u32 key0,
  145. u32 lock1, u32 key1)
  146. {
  147. soc_writel(key0, dscr.base + lock0);
  148. soc_writel(key1, dscr.base + lock1);
  149. soc_writel(val, dscr.base + reg);
  150. soc_writel(0, dscr.base + lock0);
  151. soc_writel(0, dscr.base + lock1);
  152. }
  153. static void dscr_write(u32 reg, u32 val)
  154. {
  155. struct locked_reg *lock;
  156. lock = find_locked_reg(reg);
  157. if (lock)
  158. dscr_write_locked1(reg, val, lock->lockreg, lock->key);
  159. else if (dscr.kick_key[0])
  160. dscr_write_locked2(reg, val, dscr.kick_reg[0], dscr.kick_key[0],
  161. dscr.kick_reg[1], dscr.kick_key[1]);
  162. else
  163. soc_writel(val, dscr.base + reg);
  164. }
  165. /*
  166. * Drivers can use this interface to enable/disable SoC IP blocks.
  167. */
  168. void dscr_set_devstate(int id, enum dscr_devstate_t state)
  169. {
  170. struct devstate_ctl_reg *ctl;
  171. struct devstate_stat_reg *stat;
  172. struct devstate_info *info;
  173. u32 ctl_val, val;
  174. int ctl_shift, ctl_mask;
  175. unsigned long flags;
  176. if (!dscr.base)
  177. return;
  178. if (id < 0 || id >= MAX_DEVSTATE_IDS)
  179. return;
  180. info = &dscr.devstate_info[id];
  181. ctl = info->ctl;
  182. stat = info->stat;
  183. if (ctl == NULL)
  184. return;
  185. ctl_shift = ctl->shift + ctl->nbits * (id - ctl->start_id);
  186. ctl_mask = ((1 << ctl->nbits) - 1) << ctl_shift;
  187. switch (state) {
  188. case DSCR_DEVSTATE_ENABLED:
  189. ctl_val = ctl->enable << ctl_shift;
  190. break;
  191. case DSCR_DEVSTATE_DISABLED:
  192. if (ctl->enable_only)
  193. return;
  194. ctl_val = ctl->disable << ctl_shift;
  195. break;
  196. default:
  197. return;
  198. }
  199. spin_lock_irqsave(&dscr.lock, flags);
  200. val = soc_readl(dscr.base + ctl->reg);
  201. val &= ~ctl_mask;
  202. val |= ctl_val;
  203. dscr_write(ctl->reg, val);
  204. spin_unlock_irqrestore(&dscr.lock, flags);
  205. if (!stat)
  206. return;
  207. ctl_shift = stat->shift + stat->nbits * (id - stat->start_id);
  208. if (state == DSCR_DEVSTATE_ENABLED)
  209. ctl_val = stat->enable;
  210. else
  211. ctl_val = stat->disable;
  212. do {
  213. val = soc_readl(dscr.base + stat->reg);
  214. val >>= ctl_shift;
  215. val &= ((1 << stat->nbits) - 1);
  216. } while (val != ctl_val);
  217. }
  218. EXPORT_SYMBOL(dscr_set_devstate);
  219. /*
  220. * Drivers can use this to reset RMII module.
  221. */
  222. void dscr_rmii_reset(int id, int assert)
  223. {
  224. struct rmii_reset_reg *r;
  225. unsigned long flags;
  226. u32 val;
  227. if (id < 0 || id >= MAX_SOC_EMACS)
  228. return;
  229. r = &dscr.rmii_resets[id];
  230. if (r->mask == 0)
  231. return;
  232. spin_lock_irqsave(&dscr.lock, flags);
  233. val = soc_readl(dscr.base + r->reg);
  234. if (assert)
  235. dscr_write(r->reg, val | r->mask);
  236. else
  237. dscr_write(r->reg, val & ~(r->mask));
  238. spin_unlock_irqrestore(&dscr.lock, flags);
  239. }
  240. EXPORT_SYMBOL(dscr_rmii_reset);
  241. static void __init dscr_parse_devstat(struct device_node *node,
  242. void __iomem *base)
  243. {
  244. u32 val;
  245. int err;
  246. err = of_property_read_u32_array(node, "ti,dscr-devstat", &val, 1);
  247. if (!err)
  248. c6x_devstat = soc_readl(base + val);
  249. printk(KERN_INFO "DEVSTAT: %08x\n", c6x_devstat);
  250. }
  251. static void __init dscr_parse_silicon_rev(struct device_node *node,
  252. void __iomem *base)
  253. {
  254. u32 vals[3];
  255. int err;
  256. err = of_property_read_u32_array(node, "ti,dscr-silicon-rev", vals, 3);
  257. if (!err) {
  258. c6x_silicon_rev = soc_readl(base + vals[0]);
  259. c6x_silicon_rev >>= vals[1];
  260. c6x_silicon_rev &= vals[2];
  261. }
  262. }
  263. /*
  264. * Some SoCs will have a pair of fuse registers which hold
  265. * an ethernet MAC address. The "ti,dscr-mac-fuse-regs"
  266. * property is a mapping from fuse register bytes to MAC
  267. * address bytes. The expected format is:
  268. *
  269. * ti,dscr-mac-fuse-regs = <reg0 b3 b2 b1 b0
  270. * reg1 b3 b2 b1 b0>
  271. *
  272. * reg0 and reg1 are the offsets of the two fuse registers.
  273. * b3-b0 positionally represent bytes within the fuse register.
  274. * b3 is the most significant byte and b0 is the least.
  275. * Allowable values for b3-b0 are:
  276. *
  277. * 0 = fuse register byte not used in MAC address
  278. * 1-6 = index+1 into c6x_fuse_mac[]
  279. */
  280. static void __init dscr_parse_mac_fuse(struct device_node *node,
  281. void __iomem *base)
  282. {
  283. u32 vals[10], fuse;
  284. int f, i, j, err;
  285. err = of_property_read_u32_array(node, "ti,dscr-mac-fuse-regs",
  286. vals, 10);
  287. if (err)
  288. return;
  289. for (f = 0; f < 2; f++) {
  290. fuse = soc_readl(base + vals[f * 5]);
  291. for (j = (f * 5) + 1, i = 24; i >= 0; i -= 8, j++)
  292. if (vals[j] && vals[j] <= 6)
  293. c6x_fuse_mac[vals[j] - 1] = fuse >> i;
  294. }
  295. }
  296. static void __init dscr_parse_rmii_resets(struct device_node *node,
  297. void __iomem *base)
  298. {
  299. const __be32 *p;
  300. int i, size;
  301. /* look for RMII reset registers */
  302. p = of_get_property(node, "ti,dscr-rmii-resets", &size);
  303. if (p) {
  304. /* parse all the reg/mask pairs we can handle */
  305. size /= (sizeof(*p) * 2);
  306. if (size > MAX_SOC_EMACS)
  307. size = MAX_SOC_EMACS;
  308. for (i = 0; i < size; i++) {
  309. dscr.rmii_resets[i].reg = be32_to_cpup(p++);
  310. dscr.rmii_resets[i].mask = be32_to_cpup(p++);
  311. }
  312. }
  313. }
  314. static void __init dscr_parse_privperm(struct device_node *node,
  315. void __iomem *base)
  316. {
  317. u32 vals[2];
  318. int err;
  319. err = of_property_read_u32_array(node, "ti,dscr-privperm", vals, 2);
  320. if (err)
  321. return;
  322. dscr_write(vals[0], vals[1]);
  323. }
  324. /*
  325. * SoCs may have "locked" DSCR registers which can only be written
  326. * to only after writing a key value to a lock registers. These
  327. * regisers can be described with the "ti,dscr-locked-regs" property.
  328. * This property provides a list of register descriptions with each
  329. * description consisting of three values.
  330. *
  331. * ti,dscr-locked-regs = <reg0 lockreg0 key0
  332. * ...
  333. * regN lockregN keyN>;
  334. *
  335. * reg is the offset of the locked register
  336. * lockreg is the offset of the lock register
  337. * key is the unlock key written to lockreg
  338. *
  339. */
  340. static void __init dscr_parse_locked_regs(struct device_node *node,
  341. void __iomem *base)
  342. {
  343. struct locked_reg *r;
  344. const __be32 *p;
  345. int i, size;
  346. p = of_get_property(node, "ti,dscr-locked-regs", &size);
  347. if (p) {
  348. /* parse all the register descriptions we can handle */
  349. size /= (sizeof(*p) * 3);
  350. if (size > MAX_LOCKED_REGS)
  351. size = MAX_LOCKED_REGS;
  352. for (i = 0; i < size; i++) {
  353. r = &dscr.locked[i];
  354. r->reg = be32_to_cpup(p++);
  355. r->lockreg = be32_to_cpup(p++);
  356. r->key = be32_to_cpup(p++);
  357. }
  358. }
  359. }
  360. /*
  361. * SoCs may have DSCR registers which are only write enabled after
  362. * writing specific key values to two registers. The two key registers
  363. * and the key values can be parsed from a "ti,dscr-kick-regs"
  364. * propety with the following layout:
  365. *
  366. * ti,dscr-kick-regs = <kickreg0 key0 kickreg1 key1>
  367. *
  368. * kickreg is the offset of the "kick" register
  369. * key is the value which unlocks writing for protected regs
  370. */
  371. static void __init dscr_parse_kick_regs(struct device_node *node,
  372. void __iomem *base)
  373. {
  374. u32 vals[4];
  375. int err;
  376. err = of_property_read_u32_array(node, "ti,dscr-kick-regs", vals, 4);
  377. if (!err) {
  378. dscr.kick_reg[0] = vals[0];
  379. dscr.kick_key[0] = vals[1];
  380. dscr.kick_reg[1] = vals[2];
  381. dscr.kick_key[1] = vals[3];
  382. }
  383. }
  384. /*
  385. * SoCs may provide controls to enable/disable individual IP blocks. These
  386. * controls in the DSCR usually control pin drivers but also may control
  387. * clocking and or resets. The device tree is used to describe the bitfields
  388. * in registers used to control device state. The number of bits and their
  389. * values may vary even within the same register.
  390. *
  391. * The layout of these bitfields is described by the ti,dscr-devstate-ctl-regs
  392. * property. This property is a list where each element describes a contiguous
  393. * range of control fields with like properties. Each element of the list
  394. * consists of 7 cells with the following values:
  395. *
  396. * start_id num_ids reg enable disable start_bit nbits
  397. *
  398. * start_id is device id for the first device control in the range
  399. * num_ids is the number of device controls in the range
  400. * reg is the offset of the register holding the control bits
  401. * enable is the value to enable a device
  402. * disable is the value to disable a device (0xffffffff if cannot disable)
  403. * start_bit is the bit number of the first bit in the range
  404. * nbits is the number of bits per device control
  405. */
  406. static void __init dscr_parse_devstate_ctl_regs(struct device_node *node,
  407. void __iomem *base)
  408. {
  409. struct devstate_ctl_reg *r;
  410. const __be32 *p;
  411. int i, j, size;
  412. p = of_get_property(node, "ti,dscr-devstate-ctl-regs", &size);
  413. if (p) {
  414. /* parse all the ranges we can handle */
  415. size /= (sizeof(*p) * 7);
  416. if (size > MAX_DEVCTL_REGS)
  417. size = MAX_DEVCTL_REGS;
  418. for (i = 0; i < size; i++) {
  419. r = &dscr.devctl[i];
  420. r->start_id = be32_to_cpup(p++);
  421. r->num_ids = be32_to_cpup(p++);
  422. r->reg = be32_to_cpup(p++);
  423. r->enable = be32_to_cpup(p++);
  424. r->disable = be32_to_cpup(p++);
  425. if (r->disable == 0xffffffff)
  426. r->enable_only = 1;
  427. r->shift = be32_to_cpup(p++);
  428. r->nbits = be32_to_cpup(p++);
  429. for (j = r->start_id;
  430. j < (r->start_id + r->num_ids);
  431. j++)
  432. dscr.devstate_info[j].ctl = r;
  433. }
  434. }
  435. }
  436. /*
  437. * SoCs may provide status registers indicating the state (enabled/disabled) of
  438. * devices on the SoC. The device tree is used to describe the bitfields in
  439. * registers used to provide device status. The number of bits and their
  440. * values used to provide status may vary even within the same register.
  441. *
  442. * The layout of these bitfields is described by the ti,dscr-devstate-stat-regs
  443. * property. This property is a list where each element describes a contiguous
  444. * range of status fields with like properties. Each element of the list
  445. * consists of 7 cells with the following values:
  446. *
  447. * start_id num_ids reg enable disable start_bit nbits
  448. *
  449. * start_id is device id for the first device status in the range
  450. * num_ids is the number of devices covered by the range
  451. * reg is the offset of the register holding the status bits
  452. * enable is the value indicating device is enabled
  453. * disable is the value indicating device is disabled
  454. * start_bit is the bit number of the first bit in the range
  455. * nbits is the number of bits per device status
  456. */
  457. static void __init dscr_parse_devstate_stat_regs(struct device_node *node,
  458. void __iomem *base)
  459. {
  460. struct devstate_stat_reg *r;
  461. const __be32 *p;
  462. int i, j, size;
  463. p = of_get_property(node, "ti,dscr-devstate-stat-regs", &size);
  464. if (p) {
  465. /* parse all the ranges we can handle */
  466. size /= (sizeof(*p) * 7);
  467. if (size > MAX_DEVSTAT_REGS)
  468. size = MAX_DEVSTAT_REGS;
  469. for (i = 0; i < size; i++) {
  470. r = &dscr.devstat[i];
  471. r->start_id = be32_to_cpup(p++);
  472. r->num_ids = be32_to_cpup(p++);
  473. r->reg = be32_to_cpup(p++);
  474. r->enable = be32_to_cpup(p++);
  475. r->disable = be32_to_cpup(p++);
  476. r->shift = be32_to_cpup(p++);
  477. r->nbits = be32_to_cpup(p++);
  478. for (j = r->start_id;
  479. j < (r->start_id + r->num_ids);
  480. j++)
  481. dscr.devstate_info[j].stat = r;
  482. }
  483. }
  484. }
  485. static struct of_device_id dscr_ids[] __initdata = {
  486. { .compatible = "ti,c64x+dscr" },
  487. {}
  488. };
  489. /*
  490. * Probe for DSCR area.
  491. *
  492. * This has to be done early on in case timer or interrupt controller
  493. * needs something. e.g. On C6455 SoC, timer must be enabled through
  494. * DSCR before it is functional.
  495. */
  496. void __init dscr_probe(void)
  497. {
  498. struct device_node *node;
  499. void __iomem *base;
  500. spin_lock_init(&dscr.lock);
  501. node = of_find_matching_node(NULL, dscr_ids);
  502. if (!node)
  503. return;
  504. base = of_iomap(node, 0);
  505. if (!base) {
  506. of_node_put(node);
  507. return;
  508. }
  509. dscr.base = base;
  510. dscr_parse_devstat(node, base);
  511. dscr_parse_silicon_rev(node, base);
  512. dscr_parse_mac_fuse(node, base);
  513. dscr_parse_rmii_resets(node, base);
  514. dscr_parse_locked_regs(node, base);
  515. dscr_parse_kick_regs(node, base);
  516. dscr_parse_devstate_ctl_regs(node, base);
  517. dscr_parse_devstate_stat_regs(node, base);
  518. dscr_parse_privperm(node, base);
  519. }