xref: /openbmc/qemu/hw/arm/palm.c (revision 71efffbc)
1 /*
2  * PalmOne's (TM) PDAs.
3  *
4  * Copyright (C) 2006-2007 Andrzej Zaborowski  <balrog@zabor.org>
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation; either version 2 or
9  * (at your option) version 3 of the License.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program; if not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #include "qemu/osdep.h"
21 #include "qapi/error.h"
22 #include "audio/audio.h"
23 #include "sysemu/sysemu.h"
24 #include "sysemu/qtest.h"
25 #include "ui/console.h"
26 #include "hw/arm/omap.h"
27 #include "hw/boards.h"
28 #include "hw/arm/boot.h"
29 #include "hw/input/tsc2xxx.h"
30 #include "hw/irq.h"
31 #include "hw/loader.h"
32 #include "cpu.h"
33 #include "qemu/cutils.h"
34 #include "qom/object.h"
35 #include "qemu/error-report.h"
36 
37 
38 static uint64_t static_read(void *opaque, hwaddr offset, unsigned size)
39 {
40     uint32_t *val = (uint32_t *)opaque;
41     uint32_t sizemask = 7 >> size;
42 
43     return *val >> ((offset & sizemask) << 3);
44 }
45 
46 static void static_write(void *opaque, hwaddr offset, uint64_t value,
47                          unsigned size)
48 {
49 #ifdef SPY
50     printf("%s: value %08lx written at " PA_FMT "\n",
51                     __func__, value, offset);
52 #endif
53 }
54 
55 static const MemoryRegionOps static_ops = {
56     .read = static_read,
57     .write = static_write,
58     .valid.min_access_size = 1,
59     .valid.max_access_size = 4,
60     .endianness = DEVICE_NATIVE_ENDIAN,
61 };
62 
63 /* Palm Tunsgten|E support */
64 
65 /* Shared GPIOs */
66 #define PALMTE_USBDETECT_GPIO   0
67 #define PALMTE_USB_OR_DC_GPIO   1
68 #define PALMTE_TSC_GPIO                 4
69 #define PALMTE_PINTDAV_GPIO     6
70 #define PALMTE_MMC_WP_GPIO      8
71 #define PALMTE_MMC_POWER_GPIO   9
72 #define PALMTE_HDQ_GPIO                 11
73 #define PALMTE_HEADPHONES_GPIO  14
74 #define PALMTE_SPEAKER_GPIO     15
75 /* MPU private GPIOs */
76 #define PALMTE_DC_GPIO          2
77 #define PALMTE_MMC_SWITCH_GPIO  4
78 #define PALMTE_MMC1_GPIO        6
79 #define PALMTE_MMC2_GPIO        7
80 #define PALMTE_MMC3_GPIO        11
81 
82 static MouseTransformInfo palmte_pointercal = {
83     .x = 320,
84     .y = 320,
85     .a = { -5909, 8, 22465308, 104, 7644, -1219972, 65536 },
86 };
87 
88 static void palmte_microwire_setup(struct omap_mpu_state_s *cpu)
89 {
90     uWireSlave *tsc;
91 
92     tsc = tsc2102_init(qdev_get_gpio_in(cpu->gpio, PALMTE_PINTDAV_GPIO));
93 
94     omap_uwire_attach(cpu->microwire, tsc, 0);
95     omap_mcbsp_i2s_attach(cpu->mcbsp1, tsc210x_codec(tsc));
96 
97     tsc210x_set_transform(tsc, &palmte_pointercal);
98 }
99 
100 static struct {
101     int row;
102     int column;
103 } palmte_keymap[0x80] = {
104     [0 ... 0x7f] = { -1, -1 },
105     [0x3b] = { 0, 0 },  /* F1   -> Calendar */
106     [0x3c] = { 1, 0 },  /* F2   -> Contacts */
107     [0x3d] = { 2, 0 },  /* F3   -> Tasks List */
108     [0x3e] = { 3, 0 },  /* F4   -> Note Pad */
109     [0x01] = { 4, 0 },  /* Esc  -> Power */
110     [0x4b] = { 0, 1 },  /*         Left */
111     [0x50] = { 1, 1 },  /*         Down */
112     [0x48] = { 2, 1 },  /*         Up */
113     [0x4d] = { 3, 1 },  /*         Right */
114     [0x4c] = { 4, 1 },  /*         Centre */
115     [0x39] = { 4, 1 },  /* Spc  -> Centre */
116 };
117 
118 static void palmte_button_event(void *opaque, int keycode)
119 {
120     struct omap_mpu_state_s *cpu = opaque;
121 
122     if (palmte_keymap[keycode & 0x7f].row != -1)
123         omap_mpuio_key(cpu->mpuio,
124                         palmte_keymap[keycode & 0x7f].row,
125                         palmte_keymap[keycode & 0x7f].column,
126                         !(keycode & 0x80));
127 }
128 
129 /*
130  * Encapsulation of some GPIO line behaviour for the Palm board
131  *
132  * QEMU interface:
133  *  + unnamed GPIO inputs 0..6: for the various miscellaneous input lines
134  */
135 
136 #define TYPE_PALM_MISC_GPIO "palm-misc-gpio"
137 OBJECT_DECLARE_SIMPLE_TYPE(PalmMiscGPIOState, PALM_MISC_GPIO)
138 
139 struct PalmMiscGPIOState {
140     SysBusDevice parent_obj;
141 };
142 
143 static void palmte_onoff_gpios(void *opaque, int line, int level)
144 {
145     switch (line) {
146     case 0:
147         printf("%s: current to MMC/SD card %sabled.\n",
148                         __func__, level ? "dis" : "en");
149         break;
150     case 1:
151         printf("%s: internal speaker amplifier %s.\n",
152                         __func__, level ? "down" : "on");
153         break;
154 
155     /* These LCD & Audio output signals have not been identified yet.  */
156     case 2:
157     case 3:
158     case 4:
159         printf("%s: LCD GPIO%i %s.\n",
160                         __func__, line - 1, level ? "high" : "low");
161         break;
162     case 5:
163     case 6:
164         printf("%s: Audio GPIO%i %s.\n",
165                         __func__, line - 4, level ? "high" : "low");
166         break;
167     }
168 }
169 
170 static void palm_misc_gpio_init(Object *obj)
171 {
172     DeviceState *dev = DEVICE(obj);
173 
174     qdev_init_gpio_in(dev, palmte_onoff_gpios, 7);
175 }
176 
177 static const TypeInfo palm_misc_gpio_info = {
178     .name = TYPE_PALM_MISC_GPIO,
179     .parent = TYPE_SYS_BUS_DEVICE,
180     .instance_size = sizeof(PalmMiscGPIOState),
181     .instance_init = palm_misc_gpio_init,
182     /*
183      * No class init required: device has no internal state so does not
184      * need to set up reset or vmstate, and has no realize method.
185      */
186 };
187 
188 static void palmte_gpio_setup(struct omap_mpu_state_s *cpu)
189 {
190     DeviceState *misc_gpio;
191 
192     misc_gpio = sysbus_create_simple(TYPE_PALM_MISC_GPIO, -1, NULL);
193 
194     omap_mmc_handlers(cpu->mmc,
195                     qdev_get_gpio_in(cpu->gpio, PALMTE_MMC_WP_GPIO),
196                     qemu_irq_invert(omap_mpuio_in_get(cpu->mpuio)
197                             [PALMTE_MMC_SWITCH_GPIO]));
198 
199     qdev_connect_gpio_out(cpu->gpio, PALMTE_MMC_POWER_GPIO,
200                           qdev_get_gpio_in(misc_gpio, 0));
201     qdev_connect_gpio_out(cpu->gpio, PALMTE_SPEAKER_GPIO,
202                           qdev_get_gpio_in(misc_gpio, 1));
203     qdev_connect_gpio_out(cpu->gpio, 11, qdev_get_gpio_in(misc_gpio, 2));
204     qdev_connect_gpio_out(cpu->gpio, 12, qdev_get_gpio_in(misc_gpio, 3));
205     qdev_connect_gpio_out(cpu->gpio, 13, qdev_get_gpio_in(misc_gpio, 4));
206     omap_mpuio_out_set(cpu->mpuio, 1, qdev_get_gpio_in(misc_gpio, 5));
207     omap_mpuio_out_set(cpu->mpuio, 3, qdev_get_gpio_in(misc_gpio, 6));
208 
209     /* Reset some inputs to initial state.  */
210     qemu_irq_lower(qdev_get_gpio_in(cpu->gpio, PALMTE_USBDETECT_GPIO));
211     qemu_irq_lower(qdev_get_gpio_in(cpu->gpio, PALMTE_USB_OR_DC_GPIO));
212     qemu_irq_lower(qdev_get_gpio_in(cpu->gpio, 4));
213     qemu_irq_lower(qdev_get_gpio_in(cpu->gpio, PALMTE_HEADPHONES_GPIO));
214     qemu_irq_lower(omap_mpuio_in_get(cpu->mpuio)[PALMTE_DC_GPIO]);
215     qemu_irq_raise(omap_mpuio_in_get(cpu->mpuio)[6]);
216     qemu_irq_raise(omap_mpuio_in_get(cpu->mpuio)[7]);
217     qemu_irq_raise(omap_mpuio_in_get(cpu->mpuio)[11]);
218 }
219 
220 static struct arm_boot_info palmte_binfo = {
221     .loader_start = OMAP_EMIFF_BASE,
222     .ram_size = 0x02000000,
223     .board_id = 0x331,
224 };
225 
226 static void palmte_init(MachineState *machine)
227 {
228     MemoryRegion *address_space_mem = get_system_memory();
229     struct omap_mpu_state_s *mpu;
230     int flash_size = 0x00800000;
231     static uint32_t cs0val = 0xffffffff;
232     static uint32_t cs1val = 0x0000e1a0;
233     static uint32_t cs2val = 0x0000e1a0;
234     static uint32_t cs3val = 0xe1a0e1a0;
235     int rom_size, rom_loaded = 0;
236     MachineClass *mc = MACHINE_GET_CLASS(machine);
237     MemoryRegion *flash = g_new(MemoryRegion, 1);
238     MemoryRegion *cs = g_new(MemoryRegion, 4);
239 
240     if (machine->ram_size != mc->default_ram_size) {
241         char *sz = size_to_str(mc->default_ram_size);
242         error_report("Invalid RAM size, should be %s", sz);
243         g_free(sz);
244         exit(EXIT_FAILURE);
245     }
246 
247     memory_region_add_subregion(address_space_mem, OMAP_EMIFF_BASE,
248                                 machine->ram);
249 
250     mpu = omap310_mpu_init(machine->ram, machine->cpu_type);
251 
252     /* External Flash (EMIFS) */
253     memory_region_init_rom(flash, NULL, "palmte.flash", flash_size,
254                            &error_fatal);
255     memory_region_add_subregion(address_space_mem, OMAP_CS0_BASE, flash);
256 
257     memory_region_init_io(&cs[0], NULL, &static_ops, &cs0val, "palmte-cs0",
258                           OMAP_CS0_SIZE - flash_size);
259     memory_region_add_subregion(address_space_mem, OMAP_CS0_BASE + flash_size,
260                                 &cs[0]);
261     memory_region_init_io(&cs[1], NULL, &static_ops, &cs1val, "palmte-cs1",
262                           OMAP_CS1_SIZE);
263     memory_region_add_subregion(address_space_mem, OMAP_CS1_BASE, &cs[1]);
264     memory_region_init_io(&cs[2], NULL, &static_ops, &cs2val, "palmte-cs2",
265                           OMAP_CS2_SIZE);
266     memory_region_add_subregion(address_space_mem, OMAP_CS2_BASE, &cs[2]);
267     memory_region_init_io(&cs[3], NULL, &static_ops, &cs3val, "palmte-cs3",
268                           OMAP_CS3_SIZE);
269     memory_region_add_subregion(address_space_mem, OMAP_CS3_BASE, &cs[3]);
270 
271     palmte_microwire_setup(mpu);
272 
273     qemu_add_kbd_event_handler(palmte_button_event, mpu);
274 
275     palmte_gpio_setup(mpu);
276 
277     /* Setup initial (reset) machine state */
278     if (nb_option_roms) {
279         rom_size = get_image_size(option_rom[0].name);
280         if (rom_size > flash_size) {
281             fprintf(stderr, "%s: ROM image too big (%x > %x)\n",
282                             __func__, rom_size, flash_size);
283             rom_size = 0;
284         }
285         if (rom_size > 0) {
286             rom_size = load_image_targphys(option_rom[0].name, OMAP_CS0_BASE,
287                                            flash_size);
288             rom_loaded = 1;
289         }
290         if (rom_size < 0) {
291             fprintf(stderr, "%s: error loading '%s'\n",
292                             __func__, option_rom[0].name);
293         }
294     }
295 
296     if (!rom_loaded && !machine->kernel_filename && !qtest_enabled()) {
297         fprintf(stderr, "Kernel or ROM image must be specified\n");
298         exit(1);
299     }
300 
301     /* Load the kernel.  */
302     arm_load_kernel(mpu->cpu, machine, &palmte_binfo);
303 }
304 
305 static void palmte_machine_init(MachineClass *mc)
306 {
307     mc->desc = "Palm Tungsten|E aka. Cheetah PDA (OMAP310)";
308     mc->init = palmte_init;
309     mc->ignore_memory_transaction_failures = true;
310     mc->default_cpu_type = ARM_CPU_TYPE_NAME("ti925t");
311     mc->default_ram_size = 0x02000000;
312     mc->default_ram_id = "omap1.dram";
313 
314     machine_add_audiodev_property(mc);
315 }
316 
317 DEFINE_MACHINE("cheetah", palmte_machine_init)
318 
319 static void palm_register_types(void)
320 {
321     type_register_static(&palm_misc_gpio_info);
322 }
323 
324 type_init(palm_register_types)
325