1 /* 2 * Export a JZ4730 PWM peripheral as a server. 3 * 4 * Copyright (C) 2018, 2020 Paul Boddie <paul@boddie.org.uk> 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 of 9 * the License, or (at your option) any later version. 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 17 * along with this program; if not, write to the Free Software 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, 19 * Boston, MA 02110-1301, USA 20 */ 21 22 #include <l4/devices/gpio-jz4730.h> 23 #include <l4/devices/pwm-jz4730.h> 24 #include <l4/devices/memory.h> 25 26 #include <l4/re/env.h> 27 28 #include <stdint.h> 29 #include <stdlib.h> 30 31 #include <ipc/server.h> 32 #include "pwm_object_server.h" 33 34 /* Virtual addresses for the GPIO and PWM register blocks. */ 35 36 static l4_addr_t gpio_virt_base = 0, gpio_virt_base_end = 0; 37 static l4_addr_t pwm_virt_base = 0, pwm_virt_base_end = 0; 38 39 40 41 static int setup_memory(void) 42 { 43 if (get_memory("jz4730-gpio", &gpio_virt_base, &gpio_virt_base_end)) 44 return 1; 45 46 if (get_memory("jz4730-pwm", &pwm_virt_base, &pwm_virt_base_end)) 47 return 1; 48 49 return 0; 50 } 51 52 53 54 /* PWM peripheral device. */ 55 56 class server_PWMObject : public PWMObject 57 { 58 Pwm_jz4730_chip *_device = 0; 59 int _duty, _period, _prescale; 60 61 public: 62 /* Associate the device with a particular memory region. */ 63 64 explicit server_PWMObject(Pwm_jz4730_chip *device, int duty, int period, 65 int prescale) 66 : _device(device), _duty(duty), _period(period), _prescale(prescale) 67 { 68 } 69 70 /* Disable the device. */ 71 72 long disable() 73 { 74 _device->disable(); 75 return L4_EOK; 76 } 77 78 /* Enable the device. */ 79 80 long enable() 81 { 82 set_duty(_duty); 83 set_period(_period); 84 set_control(0x80 | _prescale); /* enable | prescale */ 85 return L4_EOK; 86 } 87 88 /* Set the control register. */ 89 90 long set_control(uint8_t control) 91 { 92 _device->set_control(control); 93 return L4_EOK; 94 } 95 96 /* Set the PWM duty cycle. */ 97 98 long set_duty(uint16_t duty) 99 { 100 _duty = duty; 101 _device->set_duty(duty); 102 return L4_EOK; 103 } 104 105 /* Set the PWM period. */ 106 107 long set_period(uint16_t period) 108 { 109 _period = period; 110 _device->set_period(period); 111 return L4_EOK; 112 } 113 }; 114 115 116 117 /* Initialise devices and start the server. */ 118 119 static int run(int number, int duty, int period, int prescale) 120 { 121 Pwm_jz4730_chip pwm_device(pwm_virt_base + number * 0x1000, 122 pwm_virt_base + (number + 1) * 0x1000); 123 124 Gpio_jz4730_chip gpio_port_c(gpio_virt_base + 2 * 0x30, 125 gpio_virt_base + 3 * 0x30, 32); 126 127 /* Enable the PWM output for PC30 or PC31. */ 128 129 gpio_port_c.config_pad(30 + number, Hw::Gpio_chip::Function_alt, 1); 130 131 /* Initialise and register a new server object. */ 132 133 server_PWMObject obj(&pwm_device, duty, period, prescale); 134 l4_cap_idx_t server; 135 136 if (ipc_server_bind("pwm", (l4_umword_t) &obj, &server)) return 1; 137 138 /* Enter the IPC server loop. */ 139 140 ipc_server_loop(PWMObject_expected_items, &obj, 141 (ipc_server_handler_type) handle_PWMObject); 142 return 0; 143 } 144 145 146 147 /* Arguments: <PWM device number> <duty> <period> <prescale> */ 148 149 int main(int argc, char *argv[]) 150 { 151 int number, duty, period, prescale; 152 153 if (argc < 5) return 1; 154 155 /* Interpret and restrict the device number. */ 156 157 number = atoi(argv[1]); 158 if ((number < 0) || (number > 1)) return 1; 159 160 /* Interpret and restrict the initial parameters. */ 161 162 duty = atoi(argv[2]); 163 period = atoi(argv[3]); 164 prescale = atoi(argv[4]); 165 if ((duty < 0) || (period < 0) || (prescale < 0)) return 1; 166 167 /* Obtain access to peripheral memory. */ 168 169 if (setup_memory()) return 1; 170 171 return run(number, duty, period, prescale); 172 }