swap_case.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /*
  2. * PCI emulation device which swaps the case of text
  3. *
  4. * Copyright (c) 2014 Google, Inc
  5. * Written by Simon Glass <sjg@chromium.org>
  6. *
  7. * SPDX-License-Identifier: GPL-2.0+
  8. */
  9. #include <common.h>
  10. #include <dm.h>
  11. #include <errno.h>
  12. #include <pci.h>
  13. #include <asm/test.h>
  14. #include <linux/ctype.h>
  15. /**
  16. * struct swap_case_platdata - platform data for this device
  17. *
  18. * @command: Current PCI command value
  19. * @bar: Current base address values
  20. */
  21. struct swap_case_platdata {
  22. u16 command;
  23. u32 bar[2];
  24. };
  25. #define offset_to_barnum(offset) \
  26. (((offset) - PCI_BASE_ADDRESS_0) / sizeof(u32))
  27. enum {
  28. MEM_TEXT_SIZE = 0x100,
  29. };
  30. enum swap_case_op {
  31. OP_TO_LOWER,
  32. OP_TO_UPPER,
  33. OP_SWAP,
  34. };
  35. static struct pci_bar {
  36. int type;
  37. u32 size;
  38. } barinfo[] = {
  39. { PCI_BASE_ADDRESS_SPACE_IO, 1 },
  40. { PCI_BASE_ADDRESS_MEM_TYPE_32, MEM_TEXT_SIZE },
  41. { 0, 0 },
  42. { 0, 0 },
  43. { 0, 0 },
  44. { 0, 0 },
  45. };
  46. struct swap_case_priv {
  47. enum swap_case_op op;
  48. char mem_text[MEM_TEXT_SIZE];
  49. };
  50. static int sandbox_swap_case_get_devfn(struct udevice *dev)
  51. {
  52. struct pci_child_platdata *plat = dev_get_parent_platdata(dev);
  53. return plat->devfn;
  54. }
  55. static int sandbox_swap_case_read_config(struct udevice *emul, uint offset,
  56. ulong *valuep, enum pci_size_t size)
  57. {
  58. struct swap_case_platdata *plat = dev_get_platdata(emul);
  59. switch (offset) {
  60. case PCI_COMMAND:
  61. *valuep = plat->command;
  62. break;
  63. case PCI_HEADER_TYPE:
  64. *valuep = 0;
  65. break;
  66. case PCI_VENDOR_ID:
  67. *valuep = SANDBOX_PCI_VENDOR_ID;
  68. break;
  69. case PCI_DEVICE_ID:
  70. *valuep = SANDBOX_PCI_DEVICE_ID;
  71. break;
  72. case PCI_CLASS_DEVICE:
  73. if (size == PCI_SIZE_8) {
  74. *valuep = SANDBOX_PCI_CLASS_SUB_CODE;
  75. } else {
  76. *valuep = (SANDBOX_PCI_CLASS_CODE << 8) |
  77. SANDBOX_PCI_CLASS_SUB_CODE;
  78. }
  79. break;
  80. case PCI_CLASS_CODE:
  81. *valuep = SANDBOX_PCI_CLASS_CODE;
  82. break;
  83. case PCI_BASE_ADDRESS_0:
  84. case PCI_BASE_ADDRESS_1:
  85. case PCI_BASE_ADDRESS_2:
  86. case PCI_BASE_ADDRESS_3:
  87. case PCI_BASE_ADDRESS_4:
  88. case PCI_BASE_ADDRESS_5: {
  89. int barnum;
  90. u32 *bar, result;
  91. barnum = offset_to_barnum(offset);
  92. bar = &plat->bar[barnum];
  93. result = *bar;
  94. if (*bar == 0xffffffff) {
  95. if (barinfo[barnum].type) {
  96. result = (~(barinfo[barnum].size - 1) &
  97. PCI_BASE_ADDRESS_IO_MASK) |
  98. PCI_BASE_ADDRESS_SPACE_IO;
  99. } else {
  100. result = (~(barinfo[barnum].size - 1) &
  101. PCI_BASE_ADDRESS_MEM_MASK) |
  102. PCI_BASE_ADDRESS_MEM_TYPE_32;
  103. }
  104. }
  105. debug("r bar %d=%x\n", barnum, result);
  106. *valuep = result;
  107. break;
  108. }
  109. }
  110. return 0;
  111. }
  112. static int sandbox_swap_case_write_config(struct udevice *emul, uint offset,
  113. ulong value, enum pci_size_t size)
  114. {
  115. struct swap_case_platdata *plat = dev_get_platdata(emul);
  116. switch (offset) {
  117. case PCI_COMMAND:
  118. plat->command = value;
  119. break;
  120. case PCI_BASE_ADDRESS_0:
  121. case PCI_BASE_ADDRESS_1: {
  122. int barnum;
  123. u32 *bar;
  124. barnum = offset_to_barnum(offset);
  125. bar = &plat->bar[barnum];
  126. debug("w bar %d=%lx\n", barnum, value);
  127. *bar = value;
  128. break;
  129. }
  130. }
  131. return 0;
  132. }
  133. static int sandbox_swap_case_find_bar(struct udevice *emul, unsigned int addr,
  134. int *barnump, unsigned int *offsetp)
  135. {
  136. struct swap_case_platdata *plat = dev_get_platdata(emul);
  137. int barnum;
  138. for (barnum = 0; barnum < ARRAY_SIZE(barinfo); barnum++) {
  139. unsigned int size = barinfo[barnum].size;
  140. if (addr >= plat->bar[barnum] &&
  141. addr < plat->bar[barnum] + size) {
  142. *barnump = barnum;
  143. *offsetp = addr - plat->bar[barnum];
  144. return 0;
  145. }
  146. }
  147. *barnump = -1;
  148. return -ENOENT;
  149. }
  150. static void sandbox_swap_case_do_op(enum swap_case_op op, char *str, int len)
  151. {
  152. for (; len > 0; len--, str++) {
  153. switch (op) {
  154. case OP_TO_UPPER:
  155. *str = toupper(*str);
  156. break;
  157. case OP_TO_LOWER:
  158. *str = tolower(*str);
  159. break;
  160. case OP_SWAP:
  161. if (isupper(*str))
  162. *str = tolower(*str);
  163. else
  164. *str = toupper(*str);
  165. break;
  166. }
  167. }
  168. }
  169. int sandbox_swap_case_read_io(struct udevice *dev, unsigned int addr,
  170. ulong *valuep, enum pci_size_t size)
  171. {
  172. struct swap_case_priv *priv = dev_get_priv(dev);
  173. unsigned int offset;
  174. int barnum;
  175. int ret;
  176. ret = sandbox_swap_case_find_bar(dev, addr, &barnum, &offset);
  177. if (ret)
  178. return ret;
  179. if (barnum == 0 && offset == 0)
  180. *valuep = (*valuep & ~0xff) | priv->op;
  181. return 0;
  182. }
  183. int sandbox_swap_case_write_io(struct udevice *dev, unsigned int addr,
  184. ulong value, enum pci_size_t size)
  185. {
  186. struct swap_case_priv *priv = dev_get_priv(dev);
  187. unsigned int offset;
  188. int barnum;
  189. int ret;
  190. ret = sandbox_swap_case_find_bar(dev, addr, &barnum, &offset);
  191. if (ret)
  192. return ret;
  193. if (barnum == 0 && offset == 0)
  194. priv->op = value;
  195. return 0;
  196. }
  197. static int sandbox_swap_case_map_physmem(struct udevice *dev,
  198. phys_addr_t addr, unsigned long *lenp, void **ptrp)
  199. {
  200. struct swap_case_priv *priv = dev_get_priv(dev);
  201. unsigned int offset, avail;
  202. int barnum;
  203. int ret;
  204. ret = sandbox_swap_case_find_bar(dev, addr, &barnum, &offset);
  205. if (ret)
  206. return ret;
  207. if (barnum == 1) {
  208. *ptrp = priv->mem_text + offset;
  209. avail = barinfo[1].size - offset;
  210. if (avail > barinfo[1].size)
  211. *lenp = 0;
  212. else
  213. *lenp = min(*lenp, (ulong)avail);
  214. return 0;
  215. }
  216. return -ENOENT;
  217. }
  218. static int sandbox_swap_case_unmap_physmem(struct udevice *dev,
  219. const void *vaddr, unsigned long len)
  220. {
  221. struct swap_case_priv *priv = dev_get_priv(dev);
  222. sandbox_swap_case_do_op(priv->op, (void *)vaddr, len);
  223. return 0;
  224. }
  225. struct dm_pci_emul_ops sandbox_swap_case_emul_ops = {
  226. .get_devfn = sandbox_swap_case_get_devfn,
  227. .read_config = sandbox_swap_case_read_config,
  228. .write_config = sandbox_swap_case_write_config,
  229. .read_io = sandbox_swap_case_read_io,
  230. .write_io = sandbox_swap_case_write_io,
  231. .map_physmem = sandbox_swap_case_map_physmem,
  232. .unmap_physmem = sandbox_swap_case_unmap_physmem,
  233. };
  234. static const struct udevice_id sandbox_swap_case_ids[] = {
  235. { .compatible = "sandbox,swap-case" },
  236. { }
  237. };
  238. U_BOOT_DRIVER(sandbox_swap_case_emul) = {
  239. .name = "sandbox_swap_case_emul",
  240. .id = UCLASS_PCI_EMUL,
  241. .of_match = sandbox_swap_case_ids,
  242. .ops = &sandbox_swap_case_emul_ops,
  243. .priv_auto_alloc_size = sizeof(struct swap_case_priv),
  244. .platdata_auto_alloc_size = sizeof(struct swap_case_platdata),
  245. };